Question assign an item to a Class Array by reflection if class array is null

shiyas

New member
Joined
Mar 11, 2014
Messages
1
Programming Experience
3-5
Hi All,

I have a property and filed in a class as shown below

public Class ABC
{
 public Claim[] Claims {
            get {
                return this.claimField;
            }
            set {
                this.claimField = value;
            }
        }
 private Claim[] claimField;
}

public class Claim
{
}


but when I try to assign an object of Claim class to 'Claims' property by reflection, it is throwing 'Parameter count mismatch.' exception. below is the code what I have used.


object[] indexArgs = { 0 };
 property.SetValue(parentObject, currentObject, indexArgs);


where property=Claims property
parentObject=ABC class
currentObject= Claim object

Please help me on this
Shiyas
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
Of course that doesn't work. How can you assign a Claim object to a property whose type is Claim array? That's like putting an egg where an egg carton is expected. If the type of the property is Claim[] then you can only assign a Claim[], not a Claim.

I think that it's safe to say that you shouldn't be using an array there anyway. Think about how multi-item properties are usually handled throughout the Framework. Control.Controls, ComboBox.Items, DataTable.Rows, etc, etc. Those properties are all read-only and their type is a collection. Should yours not be the same? A read/write property whose type is an array is not unheard of but it is quite rare and even more rarely backed directly by a field, e.g. TextBox.Lines.
 
Top Bottom