I have a problem while working with GetFields()
method while trying to get all the fields from a user defined class.
I have a parent class with, lets say two public fields, and another class that inherits from the parent and overrides one of the parent fields with the new
keyword.
Problem comes when I use the GetFields
method with the second class, because it's returning all the fields, including the overriden one. Is there something that I am doing wrong or a solution for only getting in this case the GoodBye
field from the Parent class and the only one Hello
field, the one from the inherited class?
This is a simple example of what I'm saying:
public class Foo
{
public string Hello = "sdfsafda";
public string GoodBye = string.Empty;
}
public class Bar : Foo
{
public new string Hello = "jl";
}
static void Main(string[] args)
{
var a = new Bar();
var derp = new List<string>();
foreach (var fieldInfo in a.GetType().GetFields())
{
derp.Add(fieldInfo.Name);
}
Console.WriteLine(derp.Count);
// writes 3 instead of 2
}
About using Properties instead of Fields, I'm afraid I can't use them right now because of limitations that are not in my hand to solve in the project I'm working on.
Aucun commentaire:
Enregistrer un commentaire