Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have class that contain the reference of one or more structure and in structure have some properties.
now I want to find the properties from the nesting object(i.e structure reference) by creating the class object and add to dictionary.
can someone help me?

thanks
tink

What I have tried:

Dictionary
Posted
Updated 15-Sep-16 0:43am
v2
Comments
Karthik_Mahalingam 15-Sep-16 7:05am    
do you need the properties of struct also in the dictionary?
Member 11859517 15-Sep-16 7:23am    
yes, property only I want in dictionary.
BillWoodruff 15-Sep-16 9:10am    
Please describe the problem you are working with completely. Your getting answers that are helpful, but, also, are wild guesses. Accessing the Properties in an instance of a Struct in an instance of a Class before compiling is a totally different thing than accessing those properties at run-time, which requires reflection.

So: is your question about run-time, or compile-time ?
Member 11859517 16-Sep-16 0:18am    
public struct MyStruct
{
public int Id { get; set; }
public string Name { get; set; }
}
public struct MyStruct1
{
public int Id1 { get; set; }
public string Name1 { get; set; }
}


internal class MyClass
{
public MyStruct myStruct;
public MyStruct myStruct1;
}

MyClass m = new MyClass();
m.myStruct.ID = 123;
m.myStruct.Name = "xyz";

m.myStruct.ID1 = 890;
m.myStruct.Name1 = "abc";

GetProperties(m);

GetProperies(object m)
{}
i want it in run-time.
Member 11859517 16-Sep-16 0:21am    
I want using the reflection.
if m contain property we can easily find out.
but here m contain another object. plz any solution?

Let say we have a struct as defined below;

internal struct MyStruct
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }



Which is used in the class;

internal class MyClass
    {
        public MyStruct myStruct { get; set; }
    }


Retrieving and updating Dictionary can be done as;

MyClass m = new MyClass();
            var required = m.myStruct.Id;

            Dictionary<int, string> dict = new Dictionary<int, string>();
            dict.Add(required, m.myStruct.Name);
 
Share this answer
 
v2
Comments
Member 11859517 15-Sep-16 6:29am    
thanks Kshirsagarji,

I want it by using reflection.
bcz i need to dosome validation.
plz...
Member 11859517 15-Sep-16 6:39am    
and in class have
MyClass
{
public MyStruc mystruct;
}
private Dictionary<string, string> dict = new Dictionary<string, string>();
MyClass myClass = new MyClass();

// Get Class properties.
foreach (var prop in myClass.GetType().GetProperties())
{
	var propValue = prop.GetValue(myClass, null).ToString();
	this.dict.Add(prop.Name, propValue);
}

// Get struct properties.
var temp = myClass.myStruct;
foreach (var prop in temp.GetType().GetProperties())
{
    var propValue = prop.GetValue(temp).ToString();
    this.dict.Add(prop.Name, propValue);
}
 
Share this answer
 
v2
Comments
Member 11859517 15-Sep-16 7:02am    
thnaks Rick.
it working only for, if myClass contain properties.
but in my case myClass have some collectons,
example:
public struct MyStruct
{
public int Id { get; set; }
public string Name { get; set; }
}
public struct MyStruct1
{
public int Id1 { get; set; }
public string Name1 { get; set; }
}


internal class MyClass
{
public MyStruct myStruct;
public MyStruct myStruct1;
}

MyClass m = new MyClass();
m.myStruct.ID = 123;
m.myStruct.Name = "xyz";

m.myStruct.ID1 = 890;
m.myStruct.Name1 = "abc";

GetProperties(m);

GetProperies(object m)
{
private Dictionary
RickZeeland 15-Sep-16 7:25am    
That's a tricky one, maybe it's simpler to change your structs to classes, the differences are not that great.
Member 11859517 15-Sep-16 8:07am    
doesn't matter whether it is class or struct. myClass will contain collection only.
RickZeeland 15-Sep-16 7:55am    
See the updated solution for struct properties !
Member 11859517 15-Sep-16 8:12am    
here, var temp = m.myStruct;
giving error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900