获取作为变量传递的表类型

问题描述 投票:0回答:1
var TemplateGroupList = _templateGTRRepository
    .GetAll()
    .Where(x => x.TemplateGroupId == template.Id)
    .ToList();

var PropertyList = new List<string>();

foreach (var item in TemplateGroupList)
{
    var tableType = Type.GetType(item.TableName);

    if (tableType != null)
    { 
        var properties = (from t in tableType.GetProperties()
                          select t.Name).ToList();
        foreach (var propertyName in properties)
        {
            PropertyList.Add($"{item.TableName}.{propertyName}");
        }
    }
}

错误:总是获取 null

tableType

c# .net entity-framework aspnetboilerplate
1个回答
0
投票

要通过

string
类名获取某个类的类型,您还需要将程序集提交到
GetType
方法。解决该问题的一种方法是按名称加载程序集,然后从程序集中按名称加载类型

var assembly = Assembly.Load("AssemblyName");

// item.TableName must be namespase plus class name
// example: "TestApp.Data.TableName"
var type = assembly.GetType(item.TableName);
© www.soinside.com 2019 - 2024. All rights reserved.