I have a class and a reference to the class by a list:
public class class1
{
public string ADR_HOME;
public string ADR_PREF_HOME;
public string TEL_WORK;
public string TEL_WORK_PREF;
public string VERSION;
}
List
I can reference an element by
string s1 = lst1[anindex].ADR_HOME; // I do not want to hard code the element name
I want to be able to reference an element by an index
string s1 = lst1[anIndex].(1) // Want to return the data in ADR_HOME
I also want to be able to get the name that is associated with
string elementName = lst1[anIndex].(1) // want to return "ADR_HOME"
Anandu G NathPosted Jan 11, 2024, 3:50 AM
Jayraj ChhayaPosted Jan 10, 2024, 6:18 AM
To reference a class element by index in C#, you can use reflection to dynamically access the properties of the class. Here's how you can achieve this:
In the code above,
GetType()is used to get the type of the object at the specified index.GetProperties()returns an array ofPropertyInfoobjects representing the properties of the class. By specifying the index of the desired property (in this case, 1), you can access the property value usingGetValue(). To retrieve the name associated with the element, you can useNameproperty of thePropertyInfoobject.Amit MohantyPosted Jan 10, 2024, 5:54 AM
Sachin SinghPosted Jan 10, 2024, 5:22 AM
The best thing would be to create indexers so that you can easily access the properties using index, this is what indexers are made for.
Naimish MakwanaPosted Jan 10, 2024, 4:45 AM
In C#, you can’t directly reference a class’s properties by index. However, you can use reflection to achieve this. Here’s an example of how you can do it:
In this code,
s1will contain the value of the property atpropertyIndexfor the element atanIndexinlst1, andelementNamewill contain the name of that property1.Please note that this code assumes that the properties in
Class1are public and have getters. If the properties are private or protected, or if they don’t have getters, you’ll need to use different methods to get their values. Also, this code doesn’t handle any exceptions that might be thrown ifanIndexorpropertyIndexare out of range, or if the property’s getter throws an exception. You might want to add some error checking code to handle these cases.Also, please note that the order of the properties returned by
GetProperties()is not guaranteed to be the same as the order in which they’re declared in the class2. If you need to access the properties in a specific order, you might need to use a different approach, such as declaring an array or list of property names and indexing into that.Thanks
Anandu G NathPosted Jan 10, 2024, 4:14 AM
Jignesh KumarPosted Jan 10, 2024, 3:58 AM
Hello Robert,
You can access class reference data as below by Index,
var index_2 = lst1[2].ADR_HOME; This line lst1[2] represent 2nd element of list. [2] you can pass as parameter so you can access by index of list. In above example it will return value "ADR_HOME_INDEX_2"
if you wish to access first element of list then you can use lst1[0] which will return "ADR_HOME_INDEX_0"