Click here to Skip to main content
15,883,938 members
Home / Discussions / C#
   

C#

 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika31-Oct-12 6:34
prasadbuddhika31-Oct-12 6:34 
GeneralRe: casting at runtime in c# Pin
Simon_Whale31-Oct-12 6:41
Simon_Whale31-Oct-12 6:41 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika31-Oct-12 6:48
prasadbuddhika31-Oct-12 6:48 
GeneralRe: casting at runtime in c# Pin
Simon_Whale31-Oct-12 6:48
Simon_Whale31-Oct-12 6:48 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika31-Oct-12 17:04
prasadbuddhika31-Oct-12 17:04 
GeneralRe: casting at runtime in c# Pin
Simon_Whale1-Nov-12 0:45
Simon_Whale1-Nov-12 0:45 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika1-Nov-12 4:24
prasadbuddhika1-Nov-12 4:24 
GeneralRe: casting at runtime in c# Pin
Simon_Whale1-Nov-12 5:05
Simon_Whale1-Nov-12 5:05 
C#
//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


modified 1-Nov-12 11:24am.

GeneralRe: casting at runtime in c# Pin
prasadbuddhika1-Nov-12 5:25
prasadbuddhika1-Nov-12 5:25 
GeneralRe: casting at runtime in c# Pin
BobJanova1-Nov-12 5:19
BobJanova1-Nov-12 5:19 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika1-Nov-12 5:28
prasadbuddhika1-Nov-12 5:28 
QuestionC# Experts Pin
Member 955692930-Oct-12 19:32
Member 955692930-Oct-12 19:32 
AnswerRe: C# Experts Pin
Pete O'Hanlon30-Oct-12 21:29
mvePete O'Hanlon30-Oct-12 21:29 
QuestionSocket read deserialization exception (fyi - problem already resolved) Pin
devvvy30-Oct-12 17:19
devvvy30-Oct-12 17:19 
QuestionMaking Serial Port Visible Pin
C-P-User-330-Oct-12 11:35
C-P-User-330-Oct-12 11:35 
AnswerRe: Making Serial Port Visible Pin
Pete O'Hanlon30-Oct-12 11:59
mvePete O'Hanlon30-Oct-12 11:59 
AnswerRe: Making Serial Port Visible Pin
Big Daddy Farang30-Oct-12 12:05
Big Daddy Farang30-Oct-12 12:05 
GeneralRe: Making Serial Port Visible Pin
C-P-User-330-Oct-12 12:33
C-P-User-330-Oct-12 12:33 
GeneralRe: Making Serial Port Visible Pin
C-P-User-330-Oct-12 13:09
C-P-User-330-Oct-12 13:09 
GeneralRe: Making Serial Port Visible Pin
Big Daddy Farang30-Oct-12 13:42
Big Daddy Farang30-Oct-12 13:42 
GeneralRe: Making Serial Port Visible Pin
C-P-User-331-Oct-12 4:43
C-P-User-331-Oct-12 4:43 
GeneralRe: Making Serial Port Visible Pin
Pete O'Hanlon31-Oct-12 5:02
mvePete O'Hanlon31-Oct-12 5:02 
GeneralRe: Making Serial Port Visible Pin
C-P-User-32-Nov-12 6:47
C-P-User-32-Nov-12 6:47 
GeneralRe: Making Serial Port Visible Pin
Big Daddy Farang31-Oct-12 5:38
Big Daddy Farang31-Oct-12 5:38 
GeneralRe: Making Serial Port Visible Pin
C-P-User-331-Oct-12 6:56
C-P-User-331-Oct-12 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.