How to determine the generic type from an object type?

I have an extension method:

public static List<object> ToModelViewObjectList<ModelViewType>(this IEnumerable<object> source)
{

    List<ModelViewType> destinationList = new List<ModelViewType>();

    PropertyInfo[] ModelViewProperties = typeof(ModelViewType).GetProperties();
    foreach (var sourceElement in source)
    {
        object destElement = Activator.CreateInstance<ModelViewType>();
        foreach (PropertyInfo sourceProperty in sourceElement.GetType().GetProperties())
        {

            if (ModelViewProperties.Select(m => m.Name).Contains(sourceProperty.Name))
            {
                destElement.GetType().GetProperty(sourceProperty.Name).SetValue(destElement, sourceProperty.GetValue(sourceElement));
            }
        }
        destinationList.Add((ModelViewType)destElement);
    }

    return destinationList.Cast<object>().ToList();
}

And I have a method with a list of the object that I want to use call extension methods in this method:

public void GridModel(IEnumerable<object> GridDataSource)        
{ 
   List<object> list = GridDataSource.ToModelViewObjectList<GridDataSource[0].GetType()>();
}

What should be written instead of GridDataSource [0] .GetType ()?

Edited by:

I have a method with an object parameter. I want to create a general list of object types.

public void CreateListByNonGenericType(object myObject)
{
    Type objType = myObject.GetType();

    var lst = System.Collections.Generic.List < objType.MakeGenericType() > ();
}

What should I write instead objType.MakeGenericType()?

+4
source share
3 answers
public void CreateListByNonGenericType(object myObject)
{
    Type objType = myObject.GetType();
    Type listType = typeof(List<>).MakeGenericType(objType);

    //We can not use here generic version of the interface, as we
    // do not know the type at compile time, so we use the non generic 
    // interface IList which will enable us to Add and Remove elements from
    // the collection
    IList lst = (IList)Activator.CreateInstance(listType);
}
+5
source

What should I write instead GridDataSource[0].GetType()?

Since you do not know the type at compile time, you need to execute everything at runtime. Therefore, you need to make a general method from the method definition and call it. Something like the following:

GridModel(IEnumerable<object> GridDataSource)        
{ 
   Type dataSourceElementType = GridDataSource[0].GetType();

   //MyExtension is a type where ToModelViewObjectList defined
   MethodInfo methodDefinition = typeof(MyExtension).GetMethod("ToModelViewObjectList");

   //https://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.makegenericmethod
   MethodInfo method = methodDefinition.MakeGenericMethod(dataSourceElementType)

   //https://msdn.microsoft.com/en-us/library/a89hcwhh
   List<object> list = (List<object>)method.Invoke(GridDataSource, null);
}

, , - ToModelViewObjectList , , , ModelViewType , . , .

public static List<object> ToModelViewObjectList(this IEnumerable<object> source, Type modelViewType)
{
    List<object> destinationList = new List<object>();

    PropertyInfo[] ModelViewProperties = modelViewType.GetProperties();
    foreach (var sourceElement in source)
    {
        object destElement = Activator.CreateInstance(modelViewType);
        foreach (PropertyInfo sourceProperty in sourceElement.GetType().GetProperties())
        {

            if (ModelViewProperties.Select(m => m.Name).Contains(sourceProperty.Name))
            {
                destElement.GetType().GetProperty(sourceProperty.Name).SetValue(destElement, sourceProperty.GetValue(sourceElement));
            }
        }
        destinationList.Add(destElement);
    }

    return destinationList;
}

, :

List<object> list = GridDataSource.ToModelViewObjectList(GridDataSource[0].GetType());

GridDataSource, - , .

+7

, "", "". , .

public void CreateListByNonGenericType(object myObject)
{
     object objType = myObject.GetType();
     var lst = new List<object>();
     lst.Add(myObject);
}

, Generic, .

public void CreateListByNonGenericType<T>(T myObject, string s)
{
    var lst = new List<T>();
    lst.Add(myObject);
}
+1

All Articles