Ambiguity between method / field

Is there any way besides renaming the field to assign it a value if its name matches its name?

As I wrote this, I thought it was possible that reflection could be used.

Reflection works, but is there a better way to do this?

FieldInfo fi = typeof (TheClass) .GetField ("TheClash");
fi.SetValue (TheClassObj, TheFieldValue);
+3
source share
4 answers

You might want to get closer to BindingsFlags , which are optional arguments to Type.GetField (). There is one for GetField and one for GetProperty. Hope this helps!

+1
source

? (AFAIK) . , :

base.fieldName = value;

vs variable, :

this.MethodName();

, , , , "foo" "Foo", , VB.

+2

Why not drop TheClassObj to print TheClass and access this property this way?

((TheClass)TheClassObj).TheField = "blah";
+1
source

Of course, reflection is a way to access fields or a method by their name. But why do you want to do this?

You can do:

public int Test{get;set;}

Then the compiler will generate a variable under the hood for you.

0
source

Source: https://habr.com/ru/post/1771542/


All Articles