简体   繁体   English

访问列表中包含的对象的特定属性 <Object> 在C#中

[英]Access specific properties of an object contained within a List<Object> in C#

I can't seem to figure out how to access specific properties of each object contained within a list. 我似乎无法弄清楚如何访问列表中包含的每个对象的特定属性。 For example, if I reference the object outside of the list (prior to passing it to the button click), I can see properties like "tag", "height", "width", etc. (all the standard properties for that type). 例如,如果我引用列表之外的对象(在将其传递给按钮单击之前),则可以看到诸如“标签”,“高度”,“宽度”等之类的属性(该类型的所有标准属性) )。 However, I cannot figure out how to access those object-specific properties once the list has been passed to my button click event. 但是,将列表传递给按钮单击事件后,我无法弄清楚如何访问那些特定于对象的属性。

Please refer to this example: 请参考以下示例:

private TextBox createTextBox(string name)
{
    // Create TextBox Code is Here
    TextBox dTextBox = new TextBox();
    dTextBox.Name = name;
    dTextBox.Tag = "sometag";
    dTextBox.Height = 12345;
    dTextBox.Width = 12345;
    return dTextBox;
}

private void some_function()
{
    var objectList = new List<Object>();
    objectList.Add(createTextBox("example1"));
    objectList.Add(createTextBox("example2"));
    objectList.Add(createTextBox("example3"));
}

private int button_click(object sender, EventArgs e, Int32 ticketGroupID, List<Object> objectList)
{
    for(int i = 0; i < objectList.Count(); i++)
    {
        Int32 valuableInfo = objectList[i].?? // Here is where I am trying to access specific properties about the object at index i in the list, such as the objects TAG, VALUE, etc. How can this be done?
        // do things with the valuable info
    };

}

Thanks in advance for any assistance. 在此先感谢您的协助。

You need to make the object strongly-typed. 您需要使object强类型。 That is, to cast it to your class : 也就是说,将其转换为您的class

Int32 valuableInfo = ((TextBox)objectList[i]).Height; //now you can access your property

Otherwise, you cannot access the properties of the class because the compiler wouldn't know what is the actual type of the object . 否则,您将无法访问类的属性,因为编译器将不知道object的实际类型是什么。 Also, your Intellisense will just deemed it as object , not your strongly-typed class (example: MyClass , or in your case, the class being TextBox ) 另外,您的Intellisense只会将其视为object ,而不是您的强类型类(例如: MyClass ,或者在您的情况下,该类为TextBox

It's a List<> which implements IEnumerable<T> , so you can make use of the OfType<T>() method to extract items that are already strongly typed and ready for you to access: 这是一个实现IEnumerable<T>List <> ,因此您可以使用OfType<T>()方法来提取已经强类型化并可以访问的项目:

var myListOfTypedObjects = myList.OfType<TextBox>();
myListOfTypedObjects.ForEach(tb => Console.Writeline(tb.Name));

You can either check type first, then cast it to TextBox later or reverse . 您可以先检查type ,然后再将其TextBoxTextBox反向

See example below: 请参见下面的示例:

foreach (var obj in objectList)
{
    // method 1: check first, cast later
    if (obj is TextBox)
    {
        Int32 valueableInfo = ((TextBox)obj).Height;
    }

    // method2: cast first, check later
    var textBox = obj as TextBox;
    if (obj != null)
    {
        Int32 valueableInfo = obj.Height;
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM