Is there any way besides renaming the field to assign it a value if its name is the same as a method?
As of writing this I was just thinking maybe reflection can be used.
Reflection works, but is there a b开发者_StackOverflow社区etter/different way of doing this?
FieldInfo fi = typeof(TheClass).GetField("TheClash"); fi.SetValue(TheClassObj, TheFieldValue);
How does it have the same name? That shouldnt (AFAIK) be commonly possible. If the problem is with a base-class, maybe:
base.fieldName = value;
If you mean method vs variable, then:
this.MethodName();
Other than that, the only way I see a problem is it you have "foo" and "Foo", and are calling from a case-insensitive language like VB.
You may want to look closer at BindingsFlags, which are optional arguments to Type.GetField(). There is one for GetField and one for GetProperty. Hope this helps!
Why not cast TheClassObj to type TheClass and access it's property that way?
((TheClass)TheClassObj).TheField = "blah";
Sure, reflection is the way to access fields or methode by their name. But why do you want to do this?
You can do:
public int Test{get;set;}
The compiler will then generate the variable under the hood for you.
精彩评论