Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 262249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:29:59+00:00 2026-05-11T22:29:59+00:00

Suppose I have two classes that both contain a static variable, XmlTag. The second

  • 0

Suppose I have two classes that both contain a static variable, XmlTag. The second class inherits from the first class. I have a template method that needs to obtain the XmlTag depending on the which type it is using. What would be the best way to accomplish this, without having to create an instance of the type? Here is an example(that won’t compile), that should hopefully illustrate what I’m talking about.

class A{
public static readonly string XmlTag = "AClass";
}

class B : A {
public static readonly string XmlTag = "BClass";
}

This method is currently invalid.. Static variables can’t be accessed from Type paramaters apparently.

string GetName<T>(T AClass) where T : A
{
    return T.XmlTag;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:29:59+00:00Added an answer on May 11, 2026 at 10:29 pm

    First off, stop thinking of generic methods as templates. They are NOT templates. They have very different behaviour from templates; your expectation that they behave like templates is possibly what is leading you astray in this situation.

    Here is a series of articles I wrote about your scenario and why it is illegal.

    http://blogs.msdn.com/ericlippert/archive/2007/06/14/calling-static-methods-on-type-variables-is-illegal-part-one.aspx

    http://blogs.msdn.com/ericlippert/archive/2007/06/18/calling-static-methods-on-type-variables-is-illegal-part-two.aspx

    http://blogs.msdn.com/ericlippert/archive/2007/06/21/calling-static-methods-on-type-variables-is-illegal-part-three.aspx

    Note that of course the “dynamic” feature that I hint at in part three is in fact shipping with C# 4.0.

    To address your actual question: “what’s the best way to do this?” Presumably you have some problem which you believe a mechanism like this would solve. This mechanism doesn’t actually exist in C#. It is impossible for us to deduce what problem you are actually trying to solve from the fact that you would like this mechanism to exist. Instead of asking “how can I make this impossible thing work in C#?” instead describe the real problem you have, and we can take a crack at trying to come up with an existing C# mechanism that better solves your real problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer (Premature) Optimisations In addition to the other answers already posted,… May 12, 2026 at 12:47 am
  • Editorial Team
    Editorial Team added an answer The difference is in the return type declaration. List<String> is… May 12, 2026 at 12:47 am
  • Editorial Team
    Editorial Team added an answer This sounds fine, I think. I imagine if you didn't… May 12, 2026 at 12:47 am

Related Questions

I want to ask a question about how you would approach a simple object-oriented
I have a question regarding how to get the white- and black-listing feature of
i just tinkered around with Google Guice for Dependency Injection and started integrating it
I have two classes that each need an instance of each other to function.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.