简体   繁体   English

如何使用反射将类型对象转换为C#中的列表。 我可以访问对象中的属性,但不能访问值,有什么建议吗?

[英]how do I convert Type Object to a List in C# with reflection. I can access the properties in the object but cannot access the values, any suggestions?

I want to be able to loop through a generic obj of type object and display the value of each property. 我希望能够遍历类型为object的通用obj并显示每个属性的值。

I have googled but can't find a way to access the value of each Object in a object array. 我已经用谷歌搜索,但是找不到一种方法来访问对象数组中每个对象的值。

This is a test application to make sure an API call returns stuff, but i want to display the data in the UI 这是一个测试应用程序,可确保API调用返回内容,但我想在UI中显示数据

Current code: 当前代码:

[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;
  }
}

} }

If you have flat objects you could do it like this: 如果您有扁平物体,可以这样进行:

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
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在C#中访问返回对象的属性 - How can i access the properties of a returned object in c# 如何访问列表 <object> 属性? - How can I access a List<object> properties? 我可以在C#中执行对象反射吗 - Can i Do object reflection in C# 如何访问 object 类型 c# 的属性? - How to access properties of the object type c#? 如何使用 ef 访问 c# 查询列表中的 object? - How can i access an object in a list in c# query with ef? 将“代码/属性/东西”与C#中的字段关联,不带反射。 我对Javascript过于灌输 - Associate “Code/Properties/Stuff” with Fields in C# without reflection. I am too indoctrinated by Javascript 如何访问列表中对象的属性? - How can I access properties of an object within a list? 如何使用Invoke,但使用C#中的object参数来访问表单属性? - How do I access form properties with Invoke, but with object parameter in C#? 使用 C# 反射,如果该对象是列表内的对象的属性,则如何获取该对象的属性及其值 - Using C# Reflection, how to get Object's properties and their values if that Object is a property of Object that is inside of the List C# 如何访问实现接口中不存在的 object 属性 - C# how can I access the object properties that not exist in the implemented interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM