casting at runtime in c#
-
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
-
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 .
-
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
-
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.