jeudi 28 avril 2016

Create a new AnonymousType instance

I'm trying to create an AnonymousType instance looks like:

new { Channel = g.Key.Channel, Comment = g.Key.Comment, Count = g.Count() }

On the dark, .NET creates an AnonymousType with a constructor that takes three arguments: String, String, Int32.

In order to create a new instance of this anonymous type, T, I do:

object[] args = new object[3];
args[0] = "arg1";
args[1] = "arg2";
args[2] = 200;
(T)Activator.CreateInstance(typeof(T), args);

.NET dumps me:

Additional information: Constructor not found in '<>f__AnonymousType2`3[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.





Aucun commentaire:

Enregistrer un commentaire