tags:

views:

49

answers:

4

I would like to assign the property MyProperty with the parameter id.

The MyProperty property is of type Object, and it may be either an Int32 or an Int64.

How could I check the type of the MyProperty field and then assign it either id or id cast as an int depending on the underlying type?

public void MyMethod(long id) {
    myClass.MyProperty
        = (typeof(MyProperty) == typeof(long))
        ? id
        : (int)id;
}
+2  A: 

You can do:

if(myClass.MyProperty is int){
.....  do int stuff
}
else
{
 ..... do long stuff.
}

Is Operator.

Kevin
+2  A: 

So you want to assign the new value based on the type of the current value? If so:

if (myClass.MyProperty is int)
{
    myClass.MyProperty = (int) id;
}
else
{
    myClass.MyProperty = id;
}

You can do this with a conditional expression, but it's a bit ugly:

myClass.MyProperty = myClass.MyProperty is int 
    ? (object) id : (int) id;

Or:

myClass.MyProperty = myClass.MyProperty is int 
    ? (object) (int) id : id;

Or to make it clear that you really, really want boxing in either case:

myClass.MyProperty = myClass.MyProperty is int 
    ? (object) (int) id : (object) id;
Jon Skeet
Wouldn't it just be easier/clearer to use Convert.ChangeType() for this? `myClass.MyProperty = Convert.ChangeType( id, myClass.MyProperty.GetType() );`
LBushkin
@LBushkin: Hmm... possibly. Neither way feels particularly nice, to be honest...
Jon Skeet
@Jon Skeet - True. The OP's requirements are a bit strange - but it seems like they can be distilled to: *"When assigning a value to MyProperty, convert the value to the same type as MyProperty"* - which the ChangeType call captures well. It avoids peppering assumptions about the compatible types in multiple places ... at the cost of potentially propagating errors if the assigned value of MyProperty already violates the expected invariant.
LBushkin
+1  A: 

As the others said, but I Recommend you use Convert instead of casting:

long l = 2147483648; // int.MaxValue + 1
int i = (int)l; // i == -2147483648 oops
i = Convert.ToInt32(l); // Overflow exception
simendsjo
Or just operate in a checked context, of course.
Jon Skeet
A: 

This question does not make sense. 1. If the property is an object you can assign whatever you want. The type of an object property is object. 2. If you want to see the underlying private field... how do you know there is an underlying field to begin with? If you do know there is a private field why don't you know its type?

If you are in the very strange second case you may do 2 things. a) Implement code in the property to do the check and conversion. b) Decorate the property with attributes that contain metadata about the underlying field and read it via reflection.

Overall your question indicates design problem so you'd best consider redesigning instead of hacking.

Stilgar
While I agree there's probably an underlying design issue, the question *does* make sense - it's trying to assign a new value with a specific type based on the current value.
Jon Skeet
Thanks for your comment, perhaps I oversimplified my question; it was based on working with LLBLGen Entities originally.
iobrien