如何在C#中通过反射将类型对象转换为列表。我可以访问对象中的属性,但无法访问值,有什么建议吗?
我希望能够循环遍历object类型的通用obj,并显示每个属性的值 我在谷歌上搜索过,但找不到访问对象数组中每个对象的值的方法 这是一个测试应用程序,用于确保API调用返回内容,但我希望在UI中显示数据 当前代码:如何在C#中通过反射将类型对象转换为列表。我可以访问对象中的属性,但无法访问值,有什么建议吗?,c#,model-view-controller,reflection,C#,Model View Controller,Reflection,我希望能够循环遍历object类型的通用obj,并显示每个属性的值 我在谷歌上搜索过,但找不到访问对象数组中每个对象的值的方法 这是一个测试应用程序,用于确保API调用返回内容,但我希望在UI中显示数据 当前代码: [Route("Home/Index/")] [HttpPost] public string Index(string strv_Params, string strv_Method) { try { #region Region Create a new ins
[Route("Home/Index/")]
[HttpPost]
public string Index(string strv_Params, string strv_Method)
{
try
{
#region Region Create a new instance of the assebly
//Declare the assembly
Assembly executingAssebbly = AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName.Contains("DLL_Example")).FirstOrDefault();
Type customerType = executingAssebbly.GetType("CLASS_EXAMPLE");
//Assebly calls the new constructor
object customerInstance = Activator.CreateInstance(customerType);
//I need to call a mathod that is used like a construcor to set some globle varables
MethodInfo setStartupProperties = customerType.GetMethod("METHOD_NAME_EXAMPLE");
//Params needed in the construtor
object[] PropertySettings = new object[3];
PropertySettings[0] = "PropertySettings";
PropertySettings[1] = "PropertySettings";
PropertySettings[2] = "PropertySettings";
//Call the Constructor to set up the assebly
setStartupProperties.Invoke(customerInstance, PropertySettings);
#endregion
//Build up a Property array from the UI
#region Region Buiild My Params
List<string> thesplit = new List<string>();
foreach (var item in strv_Params.Split(','))
{
var ahh = item.Split('|');
thesplit.Add(ahh[1]);
}
int count = thesplit.Count();
object[] paramters = new object[count];
int li = 0;
foreach (var item in thesplit)
{
if (item == "Ref")
{
paramters[li] = "";
}
else
{
paramters[li] = item;
}
li++;
}
#endregion
//Declare the Method info using the string passed from the UI
MethodInfo GetFullNameMathod = customerType.GetMethod(strv_Method);
//Call the method using reflection with the params passing in from the UI
object retur = GetFullNameMathod.Invoke(customerInstance, paramters);
//Converts object to list of objects
object[] arr_obj = (object[])retur;
IEnumerable<object> lstl_OBJ = arr_obj.ToList();
string htmlReturn = "";
foreach (object objl_THIS in lstl_OBJ)
{
//here I want to access each value in objl_THIS
}
return htmlReturn;
}
catch (Exception ex)
{
return ex.Message;
}
}
[路由(“主/索引/”)]
[HttpPost]
公共字符串索引(字符串strv_参数,字符串strv_方法)
{
尝试
{
#区域创建assebly的新实例
//宣布大会
Assembly ExecutionGasseBly=AppDomain.CurrentDomain.GetAssemblys()。其中(x=>x.FullName.Contains(“DLL_示例”)).FirstOrDefault();
Type customerType=executingassebly.GetType(“CLASS_示例”);
//Assebly调用新构造函数
对象customerInstance=Activator.CreateInstance(customerType);
//我需要调用一个像construcor一样使用的方法来设置一些全局变量
MethodInfo setStartupProperties=customerType.GetMethod(“方法名称”示例);
//构造函数中需要的参数
object[]PropertySettings=新对象[3];
PropertySettings[0]=“PropertySettings”;
PropertySettings[1]=“PropertySettings”;
PropertySettings[2]=“PropertySettings”;
//调用构造函数来设置assebly
setStartupProperties.Invoke(customerInstance、PropertySettings);
#端区
//从UI构建属性数组
#区域创建我的参数
List thesplit=新列表();
foreach(strv_参数拆分(',')中的变量项)
{
var ahh=项目分割(“|”);
添加(ahh[1]);
}
int count=split.count();
对象[]参数=新对象[计数];
int-li=0;
foreach(分部中的var项目)
{
如果(项目==“参考”)
{
参数[li]=“”;
}
其他的
{
参数[li]=项;
}
li++;
}
#端区
//使用从UI传递的字符串声明方法信息
MethodInfo GetFullNameMathod=customerType.GetMethod(strv_方法);
//使用反射调用方法,并从UI传入参数
object retur=GetFullNameMathod.Invoke(customerInstance,参数);
//将对象转换为对象列表
对象[]arr_obj=(对象[])retur;
IEnumerable lstl_OBJ=arr_OBJ.ToList();
字符串htmlReturn=“”;
foreach(对象对象对象在lstl中)
{
//这里我想访问objl_中的每个值
}
返回htmlReturn;
}
捕获(例外情况除外)
{
返回ex.消息;
}
}
}如果您有平面对象,可以这样做:
foreach (object objl_THIS in lstl_OBJ)
{
var type = objl_THIS.GetType();
var propertyInfos = type.GetProperties();
foreach(var propertyInfo in propertyInfos)
{
string name = propertyInfo.Name;
object value = propertyInfo.GetValue(objl_THIS); // <-- value
}
}
foreach(对象对象对象在lstl\u对象中)
{
var type=objl_THIS.GetType();
var propertyInfos=type.GetProperties();
foreach(propertyInfo中的var propertyInfo)
{
字符串名称=propertyInfo.name;
object value=propertyInfo.GetValue(objl_THIS);//可能重复No^只获取对象属性而不获取值。因此不获取值?这是我需要的值。上面链接中的解决方案仅提供作为propertyInfo对象的类型列表