C# 如何将propertyInfo.PropertyType作为数据类型传递,以实例化变量并将其传递给泛型函数?
我正在尝试用简洁和存储过程编写一些更通用的代码。我做了一些研究,但我在这方面遇到了困难 我有一些类似实体的类,这些实体将由一些存储过程返回(这些过程不能修改) 例如,我有以下两个类:C# 如何将propertyInfo.PropertyType作为数据类型传递,以实例化变量并将其传递给泛型函数?,c#,.net,system.reflection,propertyinfo,C#,.net,System.reflection,Propertyinfo,我正在尝试用简洁和存储过程编写一些更通用的代码。我做了一些研究,但我在这方面遇到了困难 我有一些类似实体的类,这些实体将由一些存储过程返回(这些过程不能修改) 例如,我有以下两个类: public class User { public int Id { get; set; } public string Username { get; set; } public string Email { get; set; } } public class Role {
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; }
}
存储过程通常返回一些select子句,在本例中,它返回两个select子句,每个select子句用于用户和角色的信息,因此我使用以下类来存储这些select子句
public class UserAndRoleInfo
{
public IEnumerable<User> users { get; set; }
public IEnumerable<Role> roles { get; set;}
}
公共类用户AndroleInfo
{
公共IEnumerable用户{get;set;}
公共IEnumerable角色{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;
}
}
public async Task ExecuteStoredProcedureMultiQuery(字符串cnx,字符串storedProcedure,对象参数=null),其中T:class,new()
{
使用(var conn=new SqlConnection(cnx))
{
conn.Open();
var result=param==null
?等待conn.QUERYMULTIPLESSYNC(
存储过程,
commandType:commandType.StoredProcess
).ConfigureWait(错误)
:wait conn.QueryMultipleAsync(
存储过程,
param:param,
commandType:commandType.StoredProcess
).配置等待(错误);
T res=新的T();
类型t=类型(t);
PropertyInfo[]PropInfo=t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach(propInfo中的PropertyInfo propInfo)
{
//我想做这样的事情
propInfo.PropertyType propValue=result.Read();
propInfo.SetValue(res,propValue);
}
康涅狄格州关闭();
conn.Dispose();
返回res;
}
}
这里的问题是,我不知道如何得到这个
propInfo.PropertyType propValue = result.Read<propInfo.PropertyType.GetEnumUnderlyingType()>();
propInfo.PropertyType propValue=result.Read();
我正在使用Activator.CreateInstance(propInfo.PropertyType)
,但我不确定在这种情况下如何实现它
如果有什么不清楚或需要更多信息,我会在这里回答。提前感谢。使用答案作为参考,您可以执行以下操作:
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);
}
这回答了你的问题吗@JohnathanBarclay你能告诉我如何在这种情况下应用它吗?他这么说是因为你显示了
结果。阅读你的“类似于此”的代码中的,这是你不能做的,并且链接的问题地址。@ConnorLow,是的,我只是想知道在这种情况下如何实现这一点。。因为我不知道如何使其适用于结果。请阅读。从result.GetType().GetMethod(nameof(result.Read))代码>