Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo fellow coders

I have a simple custom class in a Silverlight project.

C#
public class UserClass
{
    public string UserID { get; set; }
    public string Tipo { get; set; }
    public string WUserName { get; set; }
    public string NMec { get; set; }
    public string Area { get; set; }
    public string SubArea { get; set; }
    public string EquipaOrdem { get; set; }
    public string Linha { get; set; }
    public string UserAdm { get; set; }
    public string SessionId { get; set; }

    public UserClass()
    {}
}


In my code I have instanciated this class:
C#
UserClass newUser = new UserClass();


In the code logic I need to set some of these members but all I know is the member name as a string.

Is there anyway to set the value of a class member using it's name as some sort of parameter?

Something like:
C#
newUser("UserID") = 123;
(I'm aware that this doesn't work!)

I've been googling for the las couple hours and couldn't find a single clue on how to do it.

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 17-Jun-14 12:30pm    
The answer is: using reflection. But why? Sounds like a bad idea.
—SA
Jorge J. Martins 19-Jun-14 6:50am    
Hi Sergey

I'm parsing a flexible xml structure that comes from a REST service and I don't know in advance what members are referenced in that xml.
I could create a switch/case for every member on the class but it seemed a bad idea at the time for the class has a lot of members (above was just an example).
So far the reflection solution is working fine and I haven's noticed any performance issue on the client side.
Sergey Alexandrovich Kryukov 19-Jun-14 12:29pm    
Oh, that's right. The problem is reduced to using serialization based on reflection. Is any existing serialize suitable for you.
Damith mentioned: "avoid reflection". This is a totally wrong idea. If depends on what you are doing. For serialization this is the blessing. But, right, reflection is slow. But this performance problem is well solved, but in a very complex way: generation code on the fly, using System.Reflection.Emit. Emit helps to convert slow reflection in code, so the reflection task is done only once, and then the generated code helps to repeat the task using them metadata already extracted by reflection and cast in the code. Are you getting the idea? The most typical approach is to generate serialization assemblies on the fly.

I can tell you as one of the rare persons who really done this job: it is very difficult, required detailed knowledge of IL and hard to debug.

—SA

1 solution

you can use Reflection but it will cost large memory overhead plus slower execution. You should avoid Reflection as much as possible. if there is no alternative try below like below by using reflection.
C#
PropertyInfo propertyInfo = UserClass.GetType().GetProperty("UserID");
propertyInfo.SetValue(UserClass, Convert.ChangeType("123", propertyInfo.PropertyType),null);


or you can change your class like below
C#
public class UserClass
{
 
 public Dictionary<string,string> settings = new  Dictionary<string,string>()
		{
			{"UserID" ,""},
			{"Tipo" ,""},
			{"WUserName" ,""},
			{"NMec" ,""},
			{"Area" ,""},
			{"SubArea" ,""},
			{"EquipaOrdem" ,""},
			{"UserAdm" ,""},
			{"SessionId" ,""}
		};

}

then you can get/set values as below
C#
UserClass user= new UserClass();
user.settings["UserID"] ="test1";
string id =user.settings["UserID"];
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 17-Jun-14 12:32pm    
It would be important to warn OP against using this technique, in almost all cases.
Reflection can be used in really useful ways excluding finding anything by names, which would be bad for maintenance.
It's better not to encourage taking wrong ways.
—SA
DamithSL 17-Jun-14 12:54pm    
Correct, answer updated. Thanks SA
Jorge J. Martins 17-Jun-14 13:07pm    
Your answer has been very useful.
So far I've made it with Reflection and it is working.

The performance issue made me think.
Now that I have a working solution I can go on improving it taking the performance issue into acount.

My 5 for your answer and thanks.
DamithSL 17-Jun-14 13:15pm    
I think you voted 1 :-)
5 in the right hand side
Jorge J. Martins 18-Jun-14 7:05am    
DamithSL

I'm prety sure I voted 5, that was my intention.
You did help me solve my problem why would I vote any other way?

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