Reputation: 31
I need to create objects of a specific type at run time (or cast a that specific type).
I've got a program with a dictionary containing attribute names and an object.
Dictionary<string, Type> attributes = new Dictionary<string, Type>();
This dictionary is fed externally (during run time)
example:
attributes.Add("attrName1", typeof(string));
attributes.Add("attrName2", typeof(long));
attributes.Add("attrName3", typeof(DateTime));
attributes.Add("attrName4", typeof(ArrayList));
The program needs to retrieve data (attribute values) from a source and return to in a specific type.
ex:
The value of an attribute called "attrName1" needs to be returned as an object of type "string"
The value of an attribute called "attrName2" needs to be returned as an object of type "long"
The value of an attribute called "attrName3" needs to be returned as an object of type "DateTime"
...
I've written a function that returns an attribute as an object
public object GetTheValue(string AttribName)
{
object oReturn;
// do whatever to retreive the value of the attribute called <AttribName> and put it in oReturn
return oReturn;
}
This function is called for each attribute to retrieve it's value
object Buffer;
object Val;
foreach(KeyValuePair<string, Type> Attribute in attributes)
{
Buffer = GetTheValue(Attribute.Key);
//Here i need to cast/convert the "Buffer object to the typeof Attribute.Value
// Stuff I tried but doesn't work :(
// (Attribute.Value.GetType())Buffer;
// (typeof(Attribute.Value))Val
// Buffer as (Attribute.Value.GetType())
// Buffer as (typeof(Attribute.Value))
// Val = new (typeof(Attribute.Value))(Buffer)
}
The only option I see at the moment is using a switch statement going through all possible Types and cast the returned object to that Type.
Someone got other options or solutions?
Upvotes: 2
Views: 6281
Reputation: 1538
ChangeType working only for type that implemented IConvertable interface
only works for types that implement IConvertible interface:
For the conversion to succeed, value must implement the IConvertible interface, because the method simply wraps a call to an appropriate IConvertible method. The method requires that conversion of value to conversionType be supported.
Try to use Expressions, for example:
Dictionary<string, Type> attributes = new Dictionary<string, Type>();
attributes.Add("attrName1", typeof(string));
attributes.Add("attrName2", typeof(long));
attributes.Add("attrName3", typeof(DateTime));
attributes.Add("attrName4", typeof(ArrayList));
object[] Items = new object[4];
Items[0] = "Test";
Items[1] = 11111L;
Items[2] = new DateTime();
Items[3] = new ArrayList();
object Buffer;
object Val;
int i = 0;
foreach(KeyValuePair<string, Type> attr in attributes)
{
Buffer = Items[i++];
//Try this expression
var DataParam = Expression.Parameter(typeof(object), "Buffer");
var Body = Expression.Block(Expression.Convert(Expression.Convert(DataParam, attr.Value), attr.Value));
var Run = Expression.Lambda(Body, DataParam).Compile();
var ret = Run.DynamicInvoke(Buffer);
}
Upvotes: 1
Reputation: 9679
Why not to use something like:
Val = Convert.ChangeType(Buffer, typeof(Attribute.Value));
Upvotes: 1