Reputation: 125
Here is my problem:
I have a class A(from project A) referencing class B(from project B). Each project is built to its own assembly
public class B
{
public const string CONSTANT_B= "Anything";
}
public class A
{
public const string filedA = B.CONSTANT_B;
}
I need to get fieldA from A class and verify if its value comes from another class(here class B).
I did this :
var fieldInfo = typeof(A).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)[0];
var value = fieldInfo.GetValue(BindingFlags.Default);
The result was: 'Anything'
So how please can I get B.CONSTANT_B instead? or just to know if Class A needs reference to class B.
My goal is to remove the reference to project B if it's unused by the project A. So how can I decide to remove It or to keep It? FYI: I have both source code and binaries of these projects.
Upvotes: 1
Views: 175
Reputation: 152566
how please can I get B.CONSTANT_B instead?
You can't When you build project A the value for the field is read from the constant field of clas B and baked directly into the code. There is no metadata to query or IL to disassemble that would tell you that the value came from B.CONSTANT_B
.
The only way that you can verify that class A gets the constant from class B is by looking at the source code.
Note also that if you change the value of B.CONSTANT_B
then that change is NOT reflected in class A unless you rebuild the project.
Now if both fields were readonly
instead of constant
you could possibly disassemble the IL to see if project A references the read-only field in B, but it would still not be available via reflection.
Upvotes: 3