linq - Getting type of items of a List at run-time in c# -


I need an expression that is the type of item of each collection.

I have a collection that:

  var collection = new list & lt; Object & gt; {1,2.2, "string", 'c'};  

It is possible to get the type of item of each collection:

  var myVariable = Archive. Select (item => item.getType ())  

But I need to create this expression on run-time. I have to do something dynamically myVariable , but how ?!

Debugger shows the value of the internal expression of myVariable

  {System.Linq.Enumerable + whereSelectListIterator`2 [System OBject, System.Type]}  

EDIT1

Explanation of the problem: Suppose I have to select a property of an object . I write cat this question like this:

  var lst = new list & lt; MyClass & gt; {New MyClass {name = "myName1", value = 1}, new MyClass {name = "myName2", value = 2}}; Var oneProperty = lst. Select (item = & gt; new {SelectedProp = item.Name});  

The result is a list of unknown types in which there are 2 items: "myName1" and "myName2". Now I want to add a property which results in the type of property selected. The following code gives me a list like this:

  var oneProperty = lst.Select (item = & gt; new {selectedProp = item.Name, TypeOfProp = item.GetType ()});  

To give an expression to the structure of the organization, to make such expression in run-time and execute this expression, is the problem approved? Viewing your recent edits, here's the run-time:

item = & gt; New Anonymous Type () {SelectProp = item.name, typeOfProp = item.GetType ()}

I added comments to show how the expression is being created You can customize it according to your needs.

  [Facts] Public Zero CreateExpression () {type argType = typeof (MyClass); String propertyName = "name"; Parameter Expression Parameter Expression Parameter (argotipp, "item"); // Create "item.Name" and "item.GetType ()" expressions var propertyAccessExpression = Expression.Property (paramExpression, propertyName); Var getTypeExpression = Expression Call (parameter expiration, "GetType", type .free type); Type anonType = CreateAnonymousType & lt; String, type & gt; ("SelectedProp", "TypeOfProp"); // "SelectProp = item.name" memberbacking assignment1 = expression.band (encrypted gatefield ("selectedprap"), propertyexexation); // "TypeoffProp = ItemGetType ()" Memberbidding assignment2 = expression.band (AniTypeGetfield ("TypeoffProp"), GateTypeXpress); // "New Anonymous Type ()" Miscellaneous Construction Expression = Expression New (AntypeGet Configurator (TypeEpteptipase)); // "Type new anonymity () {SelectProp = item.name, typeOfProp = item.GetType ()}" initialization = expression .memberinit (creation accretion, assignment 1, assignment 2); // "item => type new anonymity () {SelectProp = item.name, typeOfProp = item.GetType ()}" expression expression = expression. Lambda (initialization, parameter); }  

You will need this method to create a new anonymous type:

  public static type CreateAnonymousType & lt; TFieldA, TFieldB & gt; (String field nameA, string fieldNameB) {assemblyName dynamicAssemblyName = new assembly name ("TempAssembly"); AssemblyBuilder Dynamic Assigned = AssemblyBuilder.DefineDynamicAssembly (dynamicAssemblyName, AssemblyBuilderAccess.Run); Modulebuilder dynamicmodule = dynamic assdance. Dynamic Dynamic Module ("TempAssembly"); TypeBuilder DynamicAncient Type = Dynamic Module.DiffinType ("Anonymous Type", TypeEativant.Public); Type dynamicAngine. Defined field (field name, typef (tfilde), field etatives, public); Type dynamicAngine. Defined field (field name, typef (tfildby), field atives. Public); Return the dynamic anonymous sound type. CreateTap (); }  

Comments