Need help with Reflection from static method and child-class

G

Guest

Hi,

Suppose I have the following pseudo-class-ish code:

class A
{
member1;
member2;

public static Method() { return member1; }
}

class B : A
{
public static string Xxstring = "XXXXX";
B()
{
member1 = Xxstring;
member2 = Xxstring + Xxstring;
}
}

Now if I do the following:
B.Method() it will not work (return null) because the B's constructor was
not called as a result of the operation.

So I was hoping if there was some way i can determine from inside A.Method
that it was called from B and then I could get the type in B, and then
replace the member1 call with the Xxstring.

I have 80 or so child-classes of type A in my code and was hoping i could
refactor by doing this. Thanks.
 
M

Mattias Sjögren

So I was hoping if there was some way i can determine from inside A.Method
that it was called from B

You can't, because it will actually be called on A. You really need an
instance members (or a redesign) to do what you want.

I have 80 or so child-classes of type A in my code and was hoping i could
refactor by doing this. Thanks.

There will still only be one copy of the static members in A, so you
can't give them different values for each subclass.


Mattias
 
G

Guest

I just ended up passing a string representing class type I would like to use
to my method in A.

I messed up on original code. A members were not static, B's were and
depending on which subclass of A (which would be which class of B), I set A's
member accordingly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top