【问题标题】:How could I pass a propertyInfo.PropertyType as an Datatype to instanciate a variable and pass it to a generic function?如何将 propertyInfo.PropertyType 作为数据类型传递以实例化变量并将其传递给泛型函数?
【发布时间】:2026-01-08 22:45:01
【问题描述】:

我正在尝试使用 Dapper 和存储过程编写一些更通用的代码。我做了一些研究,但我在这部分卡住了..

我有一些类的行为类似于某些存储过程将返回的实体(这些过程无法修改)。

例如,我有这两个类:

public class User 
{
     public int Id { get; set; }
     public string Username { get; set; }
     public string Email { get; set; }
}

public class Role 
{
     public int Id { get; set; }
     public string Name { get; set; }
     public string Description { get; set; }
}

存储过程通常会返回一些选择子句,对于这个例子,它返回两个选择子句,每个子句用于用户和角色的信息,所以我有下面的类来存储它们..

public class UserAndRoleInfo 
{
     public IEnumerable<User> users { get; set; }
     public IEnumerable<Role> roles { get; set;}
}

在这个项目中,我们必须使用 Dapper,所以我正在做一个通用函数,其中 T 是要返回的类,这个类具有与上面所示相同的结构,并且可以有两个或多个实体作为属性。

主要思想是获取T的属性,然后为每个属性获取select子句转换为属性类型的返回值,最后将该属性添加到将要返回的类实例中。

public async Task<T> ExecuteStoredProcedureMultiQuery<T>(string cnx, string storedProcedure, object param = null) where T : class, new()
{
     using (var conn = new SqlConnection(cnx))
     {
         conn.Open();
         var result = param == null
                ? await conn.QueryMultipleAsync(
                    storedProcedure,
                    commandType: CommandType.StoredProcedure
                ).ConfigureAwait(false)
                : await conn.QueryMultipleAsync(
                    storedProcedure,
                    param: param,
                    commandType: CommandType.StoredProcedure
                ).ConfigureAwait(false);

         T res = new T();
         Type t = typeof(T);

         PropertyInfo[] propInfos = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

         foreach(PropertyInfo propInfo in propInfos)
         {
             // I want to do something like this 
             propInfo.PropertyType propValue = result.Read<propInfo.PropertyType.GetEnumUnderlyingType()>();
             propInfo.SetValue(res, propValue);
         }

         conn.Close();
         conn.Dispose();
          
         return res;
     }
}

这里的问题是我不知道如何得到这个

propInfo.PropertyType propValue = result.Read<propInfo.PropertyType.GetEnumUnderlyingType()>();

我正在使用Activator.CreateInstance(propInfo.PropertyType),但我不确定在这种情况下如何实现它。

如果有不清楚的地方或需要更多信息,我会在这里回答。提前致谢。

【问题讨论】:

  • 这能回答你的问题吗? How do I use reflection to call a generic method?
  • @JohnathanBarclay 你能告诉我如何在这种情况下应用它吗?
  • 他这么说是因为您在“类似这样的”代码中显示result.Read&lt;propInfo.PropertyType.GetEnumUnderlyingType()&gt;,这是您无法做到的,并且链接的问题地址。
  • @ConnorLow,没错,我只是想知道在这种情况下如何实现这一目标.. 因为我不知道如何使它工作
  • this answer 应用到result.Read。从result.GetType().GetMethod(nameof(result.Read)); ....开始

标签: c# .net system.reflection propertyinfo


【解决方案1】:

使用答案here 作为参考,您可以这样做:

foreach (var propInfo in propInfos)
{
    var method = result.GetType().GetMethod(nameof(result.Read));
    var generic = method.MakeGenericMethod(propInfo.PropertyType.GetEnumUnderlyingType());
    var propValue = generic.Invoke(result, null);
    propInfo.SetValue(res, propValue);
}

【讨论】: