[英]How to get a property using reflection?
我需要从我的项目中删除对System.Data.SqlClient
的任何引用。 我使用它的唯一实例是获取错误列表,例如:
if (ex is SqlException)
{
var spx = ex as SqlException;
if (spx.Errors != null)
{
for (int i = 0; i < spx.Errors.Count; i++)
{
var sqlError = spx.Errors[i];
}
}
}
是使用Reflection
并调用GetProperty
的唯一替代方法,如下所示:
if (ex.GetType().Name == "SqlException")
{
var errors = ex.GetType().GetProperty("Errors");
if (errors != null)
{}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.