mardi 1 septembre 2015

Assigning a value to a dynamic type using reflection and bypassing the object cast\box that GetValue returns

I have two objects that have exactly the same properties. Here's an example:

public class Field<T>
{
   public T Value {get;set;}
   // more things
}

public class MainObject
{
   public string MyStr {get;set;}
   public int? MyInt {get;set;}
   public bool? MyBool {get;set;}
}

public class MainObjectField
{
   public Field<string> MyStr {get;set;}
   public Field<int?> MyInt {get;set;}
   public Field<bool?> MyBool {get;set;}
}

I have a task to create an instance of MainObjectField based on xpath instructions that I get from an external source.
I need to fetch the Value from the MainObject's instance as a part of the task.

Since I don't know about the type of the current property in compile type, I'm using a dynamic variable.

Here's a short example about how the code looks like in a high level aspect:

dynamic currentField = GetMainObjectsCurrentFieldSomehow();
object val = GetValFromMainObject(); // using reflection
currentField.Value = val; // I get an exception here

Now I'm getting this exception:

Cannot implicitly convert type 'object' to 'bool?'. An explicit conversion exists (are you missing a cast?)

I understand why I'm getting the exception and I'm trying to find a workaround.

I know that the currentField.Value and the val value are suppose to be the same type.

My problem is that using reflection (GetValue returns an object) automatically casts\boxes the value into an object and ruins my plans. I can't cast to the "correct" value since I know the types only in runtime.

How can I work this around?





Aucun commentaire:

Enregistrer un commentaire