views:

941

answers:

4

I try to set a Nullable<> property dynamicly.

I Get my property ex :

PropertyInfo property = class.GetProperty("PropertyName"); // My property is Nullable<> at this time So the type could be a string or int

I want to set my property by reflection like

property.SetValue(class,"1256",null);

It's not working when my property is a Nullable<> Generic. So i try to find a way to set my property.

To know the type of my nullable<> property i execute

Nullable.GetUnderlyingType(property.PropertyType)

Any idea ?

  • I Try to create an instance of my Nullable<> property with

    var nullVar = Activator.CreateInstance(typeof(Nullable<>).MakeGenericType(new Type[] { Nullable.GetUnderlyingType(property.PropertyType) }));

But nullVar is always Null

+4  A: 

If it's a nullable int, you'll need to use an int parameter, not a string.

 property.SetValue(klass,1256,null);

Note the change to klass, instead of class, as class is a reserved keyword. You could also use @class if absolutely necessary (quoting it).

If your property is a generic, then I think you'll probably need to use Convert to convert whatever you have to whatever you need.

 var nullType = Nullable.GetUnderlyingType(property.PropertyType)
 var value = Convert.ChangeType("1256", nullType );
 property.SetValue(klass, value, null );
tvanfosson
I know it's a string but it's generic so i dont know what will be the type. It's only an example, my genric Nullable<> could be a string, int a generic conversion will be done
Cédric Boivin
You can't have a `Nullable<string>`, since `string` is not a value type.
Ben M
+1  A: 

"1256" is a string, not an int.

Steve Gilham
+2  A: 

Here is a complete example showing how to do it:

using System;
using System.Reflection;

class Test
{
    static void Main()
    {
     Foo foo = new Foo();
     typeof(Foo).GetProperty("Bar")
      .SetValue(foo, 1234, null);
    }
}

class Foo
{
    public Nullable<Int32> Bar { get; set; }
}

As others have mentioned you need to pass the right type to the SetValue function but your other reflection code is not quite right either. You need to get the type of the class in question before you can query for its members.

Edit: If I understand correctly you are trying to set a string value to any property via reflection. In order to do this you will need to do some type inspection and type conversion.

Here is an example of what I mean:

using System;
using System.Reflection;

class Test
{
    static void Main()
    {
     Foo foo = new Foo();

     PropertyInfo property = typeof(Foo).GetProperty("Bar");
     Object value =
      Convert.ChangeType("1234",
       Nullable.GetUnderlyingType(property.PropertyType)
       ?? property.PropertyType);

     property.SetValue(foo, value, null);
    }
}

class Foo
{
    public Nullable<Int32> Bar { get; set; }
}

This approach can be safely used regardless of whether or not the property is Nullable<>.

Andrew Hare
+2  A: 

If you want to convert an arbitrary string to the underlying type of the Nullable, you can use the Convert class:

var propertyInfo = typeof(Foo).GetProperty("Bar");
object convertedValue = null;
try 
{ 
    convertedValue = System.Convert.ChangeType("1256", 
        Nullable.GetUnderlyingType(propertyInfo.PropertyType));
} 
catch (InvalidCastException)
{
    // the input string could not be converted to the target type - abort
    return;
}
propertyInfo.SetValue(fooInstance, convertedValue, null);

This example will work if the target type is int, short, long (or unsigned variants, since the input string represents a non-negative number), double, float, or decimal. Caveat: this is not fast code.

Ben M
I Will try that, at this time i tink your are the closer answer i search
Cédric Boivin
It's workin Thanks
Cédric Boivin