get the name of a parameter as a string
-
how can i get the name of a parameter as string. sample: ------------------------------------- class abc() { public int parametername = 0; public string parametername2 = String.Empty; } class ReadParameterName() { // here i want to read the name of any parameter in abc // i want select the parameter like: // abc.parametername } -------------------------------------
-
how can i get the name of a parameter as string. sample: ------------------------------------- class abc() { public int parametername = 0; public string parametername2 = String.Empty; } class ReadParameterName() { // here i want to read the name of any parameter in abc // i want select the parameter like: // abc.parametername } -------------------------------------
Reflection is how you do this.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
how can i get the name of a parameter as string. sample: ------------------------------------- class abc() { public int parametername = 0; public string parametername2 = String.Empty; } class ReadParameterName() { // here i want to read the name of any parameter in abc // i want select the parameter like: // abc.parametername } -------------------------------------
-
Hi, Describe the problem completely. do you want the list of members from class abc in to the class ReadParametername? and if u want to pass name of the parameters as string variable then why do u want to do it? thanks.
ok, i read an article about reflection! and found out som new: ------------------------------------- class abc() { public int parametername = 0; public string parametername2 = String.Empty; } class ReadParameterName() { Type TypeToReflect = typeof(abc); System.Reflection.MemberInfo[] Members = TypeToReflect.GetMembers(); // now i get an array with the members of the object 'abc' // can i get an enum of the membernames in realtime? ??? // so that i can select any membername from the enum like: string anyMemberName = enumAbcMembers.parametername.ToString(); } -------------------------------------
-
how can i get the name of a parameter as string. sample: ------------------------------------- class abc() { public int parametername = 0; public string parametername2 = String.Empty; } class ReadParameterName() { // here i want to read the name of any parameter in abc // i want select the parameter like: // abc.parametername } -------------------------------------
Hi Here is another way to do the same thing. If I'm right it's one of the refactoring practices. Use an object instead of a struct(int,string,...) You can have a parameter class: ------------- Parameter ------------- +Name:string +Value:object ------------- ------------- Then one may write
Parameter param1=new Parameter("abc",0); Parameter param2=new Parameter("anotherParam",1);
and later:string paramName=param1.Name;
I prefer this method because my intention is implied(Everyone can look at the class interface and see what's going on) Regards