我对c#反射有一个问题。我想要反映的对象如下:
public partial class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
}
public decimal CustomerId { get; set; }
public string AlexaAccessToken { get; set; }
public string GoogleHomeAccessToken { get; set; }
}我用来反映的代码如下:
Dictionary<string,string> GetReplacement(ApplicationUser applicationUser)
{
Dictionary<string, string> toRet = new Dictionary<string, string>();
PropertyInfo[] propertyInfos;
propertyInfos = typeof(ApplicationUser).GetProperties(BindingFlags.Public);
Array.Sort(propertyInfos,
delegate (PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
{ return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });
foreach (PropertyInfo propertyInfo in propertyInfos)
{
toRet.Add(propertyInfo.Name,propertyInfo.GetValue(applicationUser).ToString());
}
return toRet;
}问题是字典总是空的,因为propertyinfo总是空的。有什么问题吗?先谢谢大家。
发布于 2019-09-16 11:47:49
这里有两个问题:
BindingFlags.Public | BindingFlags.Instance绑定propertyInfo.GetValue(applicationUser)?.ToString()或Convert.ToString(propertyInfo.GetValue(applicationUser))https://stackoverflow.com/questions/57956167
复制相似问题