Generic type as variable

I have a method that takes a Generic T class

  public void CreateTables<T>()
   {
    string name = typeof(T).Name;

    var fields = typeof(T).GetProperties().Select(t => new { key = 
    t.Name.ToLower(CultureInfo.InvariantCulture), value = 
    SqLiteUtility.GetSQLiteTypeString(t.PropertyType) })
      .ToDictionary(t => t.key, t => 
          t.value);

    CreateTable(name, fields);
    }

   and 

    public void PushData<T>() where T : EntityData
   {
    var data = _context.Set<T>().Where(p => p.Deleted == false).ToList();
   }

I have more than 50 types for which this method needs to be called. Like this

CreateTables<Class1>();
PushData<Class1>();

Although I can do it, but I prefer to create, perhaps, an array of types and use it for a loop to call this method

Something like that

   Type[] types=new Types[]{typeof(Class1),typeof(Class2)}

   foreach(var type in types)
   {
    //call create table method
      CreateTables<type>(); - - this doesnt work as "type" is a variable 
                                used as a type 

   }

Is there any way around this? which can also save me a lot of code and refactoring?

EDIT1:

Of the answers, there really can be parameters such as CreateTables (Type T), however, is there an opportunity for the second method above?

In the second, it is important to note that T is referred to as EntityData, since the code in this method depends on it.

+4
source share
3 answers

Essentially, you are trying to move from reflection to generics and return to reflection.

, , :

public void CreateTables<T>()
{
    CreateTables(typeof(T));
}

public void CreateTables(Type tableType)
{
    string name = tableType.Name;

    var fields = tableType.GetProperties().Select(t => new
        {
            key = t.Name.ToLower(CultureInfo.InvariantCulture),
            value = SqLiteUtility.GetSQLiteTypeString(t.PropertyType)
        })
        .ToDictionary(
            t => t.key,
            t => t.value);

    CreateTable(name, fields);
}

, , generics-wise , .


, Type, :

var m = GetType().GetMethod("CreateTables").MakeGenericMethod(new Type[] { type });
m.Invoke(this, null);

!. , , ..

+9

- :

public void CreateTables<T>()
{
  CreateTables(typeof(T));
}

public void CreateTables(Type type)
{
  string name = type.Name;

  var fields = type.GetProperties().Select(
      t => new { 
         key = t.Name.ToLower(CultureInfo.InvariantCulture), 
         value = SqLiteUtility.GetSQLiteTypeString(t.PropertyType) 
      }).ToDictionary(
         t => t.key, 
         t => t.value);

  CreateTable(name, fields);
}

:

Type[] types=new Types[]{typeof(Class1),typeof(Class2)}

foreach(var type in types)
{
    CreateTables(type);
}

, ,

+7

Try the following:

public void CreateTables<T>() => CreateTables(typeof(T));

public void CreateTables(Type type)
{        
    string name = type.Name;

    var fields = type.GetProperties()
      .Select(t => new { 
          key = t.Name.ToLower(CultureInfo.InvariantCulture), 
          value = SqLiteUtility.GetSQLiteTypeString(t.PropertyType) })
      .ToDictionary(t => t.key, t => t.value);

    CreateTable(name, fields);
 }

Or even this:

public void CreateTables(IEnumerable<Type> types) {
    if(types != null) {
        foreach(Type type in types) {
            CreateTables(type);
        }
    }
}
+4
source

Source: https://habr.com/ru/post/1689002/


All Articles