i want to implement a class that one of the attribute is dynamic but i can't seem to figure out how to do that...
public class myClass
{
public datetime timeStamp { get; set; }
public object variable { get; set; }
public string name { get; set; }
}
I need to read a archive that has the type of the "variable" inside as a integer, for example
This is just an exaple of a json or something that i will use, the "variable" attribute of the clas (in this example) will be a string, but there are sometimes that this attribute should be a integer(example2)
Ex1:
{"name" : "Variable1" , "variable" : "string" : "myString" , "timeStamp" : "852852852" }
Ex2:
{"name" : "Variable2" , "variable" : "int32" : "125125" , "timeStamp" : "852852852" }
You could make OPC_UA a generic class:
class OPC_UA<T>
{
public DateTime TimeStamp { get; set; }
public T Variable { get; set; }
public string Name { get; set; }
}
And then you can instantiate the type using reflection methods:
Type type = Type.GetType("System.Int32");
Type genericizedType = typeof(OPC_UA<>).MakeGenericType(type);
dynamic opc_ua = Activator.CreateInstance(genericType);
opc_ua.Variable = 5; // For example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.