By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,780 Members | 1,523 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,780 IT Pros & Developers. It's quick & easy.

Need help with Reflection from static method and child-class

P: n/a
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.
Apr 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 5 '06 #2

P: n/a
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.

"Mattias Sjögren" wrote:
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Apr 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.