casting at runtime in c#
-
hi , I got an object which is a generic, at runtime i pass the type for T and create the proper object. the generic object as follows
class parent {
string name;
T childObject;
}I create the parent in runtime using the reflection , for e.g.
Type type = x;
Dynamic response = Activator.CreateInstance(typeof(parent<>).MakeGenericType(type));it works fine , but the problem is when I try to assign an object to response.childObject it gives me an exception saying that i'm missing cast and it cannot implicitly cast the object. the object try to assign is also created using reflection, ADDED LATER: I'm not sure this part will be important for this issue , but anyway , the object i'm trying to assign to response.childObject is also an object created by deserialization , means for in order to deserialize this object i have to give the type , so that type also crated using reflection in a similar way described above. i would really appreciate your help on this. thanks in advance.
-
hi , I got an object which is a generic, at runtime i pass the type for T and create the proper object. the generic object as follows
class parent {
string name;
T childObject;
}I create the parent in runtime using the reflection , for e.g.
Type type = x;
Dynamic response = Activator.CreateInstance(typeof(parent<>).MakeGenericType(type));it works fine , but the problem is when I try to assign an object to response.childObject it gives me an exception saying that i'm missing cast and it cannot implicitly cast the object. the object try to assign is also created using reflection, ADDED LATER: I'm not sure this part will be important for this issue , but anyway , the object i'm trying to assign to response.childObject is also an object created by deserialization , means for in order to deserialize this object i have to give the type , so that type also crated using reflection in a similar way described above. i would really appreciate your help on this. thanks in advance.
I dont know how it actual works if I am honest (as I found the question interesting) but my modifying your code to this
Type t = typeof(Parent<>);
int y = 1234;
Type type = y.GetType();dynamic response = Activator.CreateInstance(t.MakeGenericType(type));
response.MyValue = 123;public class Parent
{
public string name = string.Empty;
public T MyValue;
}I was able to assign a value to the property MyValue. *Edit* Not sure if it helps but I've just read through this MSDN article on Type.MakeGenericType[^], I found this an interesting Question :-D
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
-
I dont know how it actual works if I am honest (as I found the question interesting) but my modifying your code to this
Type t = typeof(Parent<>);
int y = 1234;
Type type = y.GetType();dynamic response = Activator.CreateInstance(t.MakeGenericType(type));
response.MyValue = 123;public class Parent
{
public string name = string.Empty;
public T MyValue;
}I was able to assign a value to the property MyValue. *Edit* Not sure if it helps but I've just read through this MSDN article on Type.MakeGenericType[^], I found this an interesting Question :-D
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
thanks , I also tried the value types and it works , but the problem came when i'm trying to assign reference type.
-
thanks , I also tried the value types and it works , but the problem came when i'm trying to assign reference type.
Type t = typeof(Parent<>);
Type type = typeof(Data);dynamic response = Activator.CreateInstance(t.MakeGenericType(type));
Data es = new Data() { Name = "Fred", value = 213 };
response.value = es;public class Parent
{
public string name = string.Empty;
public T value;
}public class Data { public string Name = string.Empty; public int value = 0; }
I have also tried it with the following and it still works can you expand your problem with some sample code? or a snippet?
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
-
Thanks again , ok then I think I have to focus on the deserialzed object , unfortunately i haven't got the code snippet right now , but I will definitely post that code snippet soon. thanks .
Your welcome :-D
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
-
Type t = typeof(Parent<>);
Type type = typeof(Data);dynamic response = Activator.CreateInstance(t.MakeGenericType(type));
Data es = new Data() { Name = "Fred", value = 213 };
response.value = es;public class Parent
{
public string name = string.Empty;
public T value;
}public class Data { public string Name = string.Empty; public int value = 0; }
I have also tried it with the following and it still works can you expand your problem with some sample code? or a snippet?
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
Thanks again , ok then I think I have to focus on the deserialzed object , unfortunately i haven't got the code snippet right now , but I will definitely post that code snippet soon. thanks .
-
Your welcome :-D
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
hi , this is how i'm doing the deserialization
Type genericSyncObj = typeof(SyncObject<>);
Type syncObj = genericSyncObj.MakeGenericType(type);
IList customObjectList = (IList)Activator.CreateInstance((typeof(List<>).MakeGenericType(type)));foreach (string value in pullRequest.Knowledge)
{
dynamic syncObject = JasonSerializer.Deserialize(value, syncObj, null);
customObjectList.Add(syncObject.Object);
}and i'm getting the exception at here
dynamic responseSync = Activator.CreateInstance(typeof(SyncObject<>).MakeGenericType(type));
responseSync.Object = customObjectList[y] ;thanks .
-
hi , this is how i'm doing the deserialization
Type genericSyncObj = typeof(SyncObject<>);
Type syncObj = genericSyncObj.MakeGenericType(type);
IList customObjectList = (IList)Activator.CreateInstance((typeof(List<>).MakeGenericType(type)));foreach (string value in pullRequest.Knowledge)
{
dynamic syncObject = JasonSerializer.Deserialize(value, syncObj, null);
customObjectList.Add(syncObject.Object);
}and i'm getting the exception at here
dynamic responseSync = Activator.CreateInstance(typeof(SyncObject<>).MakeGenericType(type));
responseSync.Object = customObjectList[y] ;thanks .
The problem is that
customObject[y]
is returned as anobject
which is why it fails. if you then hard code a cast such as for example.//assume SomeTestClass is the type of class that you are using
//Type type = typeof(SomeTestClass);
responseSync.Object = (SomeTestClass)customObject[y];it works as you need it to do, but how to create this as a generic routine I'm not sure at the moment as
(typeof(SomeTestClass))customObject[y]
or even(typeof(SomeTestClass))CustomObject[y]
is rejected by the compiler.Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
-
The problem is that
customObject[y]
is returned as anobject
which is why it fails. if you then hard code a cast such as for example.//assume SomeTestClass is the type of class that you are using
//Type type = typeof(SomeTestClass);
responseSync.Object = (SomeTestClass)customObject[y];it works as you need it to do, but how to create this as a generic routine I'm not sure at the moment as
(typeof(SomeTestClass))customObject[y]
or even(typeof(SomeTestClass))CustomObject[y]
is rejected by the compiler.Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
thanks , yes this is the place where i'm stuck , need to cast in runtime , if you find a way please share it .
-
thanks , yes this is the place where i'm stuck , need to cast in runtime , if you find a way please share it .
//Create sample data in the same method as OP
IList datalist = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(Data)));datalist.Add(new Data {Name = "SIMON", value = 1});
datalist.Add(new Data {Name = "SIMON", value = 2});
datalist.Add(new Data {Name = "SIMON", value = 3});//Create the destination object that is declared using
//the dynamic keyword
Type t = typeof(Parent<>);
Type type = typeof(Data);dynamic response = Activator.CreateInstance(t.MakeGenericType(type));
//************ possible work around not sure if it would be a correct way
//************ but it works.Type SingleRecordType = datalist[0].GetType();
dynamic Destination = Activator.CreateInstance(SingleRecordType);
Desintation = datalist[0];//Add the record to the response object
response.value = Desintation;have a look at this. it is a bit of a work around converting the list item from object to a specific type at runtime. Thanks Simon **edit** missed a GetType()
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
-
hi , this is how i'm doing the deserialization
Type genericSyncObj = typeof(SyncObject<>);
Type syncObj = genericSyncObj.MakeGenericType(type);
IList customObjectList = (IList)Activator.CreateInstance((typeof(List<>).MakeGenericType(type)));foreach (string value in pullRequest.Knowledge)
{
dynamic syncObject = JasonSerializer.Deserialize(value, syncObj, null);
customObjectList.Add(syncObject.Object);
}and i'm getting the exception at here
dynamic responseSync = Activator.CreateInstance(typeof(SyncObject<>).MakeGenericType(type));
responseSync.Object = customObjectList[y] ;thanks .
-
//Create sample data in the same method as OP
IList datalist = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(Data)));datalist.Add(new Data {Name = "SIMON", value = 1});
datalist.Add(new Data {Name = "SIMON", value = 2});
datalist.Add(new Data {Name = "SIMON", value = 3});//Create the destination object that is declared using
//the dynamic keyword
Type t = typeof(Parent<>);
Type type = typeof(Data);dynamic response = Activator.CreateInstance(t.MakeGenericType(type));
//************ possible work around not sure if it would be a correct way
//************ but it works.Type SingleRecordType = datalist[0].GetType();
dynamic Destination = Activator.CreateInstance(SingleRecordType);
Desintation = datalist[0];//Add the record to the response object
response.value = Desintation;have a look at this. it is a bit of a work around converting the list item from object to a specific type at runtime. Thanks Simon **edit** missed a GetType()
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
hi thanks it seems to be working, though it is a workaround . thanks for the clue . ** yes i noticed that and i fixed it by calling the GetType() thanks.
-
If you're getting a runtime exception, then it's probably because the object actually isn't of the right type. The exception message should tell you what it is and what it's trying to cast it to.
hi , yes , that's the case , I was searching for a way to solve this at runtime. anyway I got a clue a from Simon_Whale. thanks.