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 6161967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:34:17+00:00 2026-05-23T21:34:17+00:00

How can I get the current Type in a static method that is defined

  • 0

How can I get the current Type in a static method that is defined in an abstract class?

Please note that as the method is defined in an abstract class, I can’t use typeof.

Why would I want to do this? A possible usage is attributes. Consider the following example:

[Identifier(1000)]
public class Rock : Entity { }

public abstract class Entity
{
    public static ushort Identifier
    {
        get
        {
            // How do I get here the current type of the object?
            Type currentType = ...;

            // in a non-static method or property, I'd do:
            // Type currentType = this.GetType();

            foreach (IdentifierAttribute attribute in currentType.GetCustomAttributes(true))
                return attribute.Identifier;

            throw new EntityException("No identifier has specified for this type of entity.");
        }
    }
}

Rock rock = new Rock();

// should print 1000
Console.WriteLine(rock.Identifier);

EDIT:

Here is the scenario.

Entity represents a 3D object. I’m writing a server software, that has a list of such entities. The server manually serializes the list and sends it to the client. As performance is highly important here, I’m not sending the type name. Every type of entity has a unique identifier, so when the client gets the data, it can efficiently deserialize it.

To create an instance of an entity, I’m doing something like:

Entity entity = EntityRepository.Instance.CreateNew(identifier);

The EntityRepository class looks like this:

public sealed class EntityRepository
{
    private static readonly Lazy<EntityRepository> lazy =
        new Lazy<EntityRepository>(() => new EntityRepository());

    IDictionary<ushort, Func<Entity>> _repo;

    private EntityRepository()
    {
        _repo = new Dictionary<ushort, Func<Entity>>();
    }

    public static EntityRepository Instance 
    { 
        get { return lazy.Value; } 
    }

    public Entity CreateNew(ushort id)
    {
        return _repo[id]();
    }

    public void Add<T>(ushort id)
        where T : Entity, new()
    {
        _repo.Add(id, new Func<Entity>(() =>
        {
            return new T();
        }));
    }
}

The current Add<T> method has a parameter that represents the identifier.

But how would I write a Add<T> method that has no parameters – that recognizes the identifier automatically?

So I was thinking about adding an attribute to the nested Entity:

[Identifier(1000)]
public class Rock : Entity { }

and a static property that returns the value of the Identifier attribute.

Then, an Add<T> method without parameters would look something like:

public void Add<T>(ushort id)
    where T : Entity, new()
{
    _repo.Add(T.Identifier, new Func<Entity>(() =>
    {
        return new T();
    }));
}

Note that in this case, I could just do T.GetType() to get the attribute, but that’s not the point. How can I do that in the static property, Entity.Identifier?

  • 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-23T21:34:18+00:00Added an answer on May 23, 2026 at 9:34 pm

    You can’t, basically.

    A call to Rock.Identifier will be resolved by the compiler to Entity.Identifier – there simply isn’t any context in which to find the type.

    A call to rock.Identifier won’t even compile, as you’re trying to access a static member through a variable.

    The best workaround would depend on the real scenario – in the case you’ve posted, I’d suggest creating a static method elsewhere which accepted a type as a parameter.

    Actually, you could fake it in a really horrible way for your specific scenario by using the compile-time type of Rock:

    public static class NastyExtensions
    {
        public static ushort GetIdentifier<T>(this T actualValueIsIgnored)
        {
            Type type = typeof(T);
            ... code as before
        }
    }
    

    Then:

    Rock rock = new Rock();
    Console.WriteLine(rock.GetIdentifier());
    

    But this would not be polymorphic. For example:

    Entity rock = new Rock();
    Console.WriteLine(rock.GetIdentifier()); // Bang! T would be Entity here
    

    You could change the extension method to call GetType() of course…

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

Sidebar

Related Questions

How can I get the current selected row for UIPicker? I want to use
Inside of a static member function I need to get the type. class MyClass
With Type.GetProperties() you can retrieve all properties of your current class and the public
I can get the current selected row in this way: private void DataGridView1_CellContentClick(object sender,
How can I get current time (I need hours, minutes, seconds) crossplatform in c++?
How can I get the current hour, minutes, and seconds from this? NSDate *now
How can I get the current system status (current CPU, RAM, free disk space,
How can I get the current local wall clock time (in number of millis
how can I get the current time in hh:mm format? I need to be
Referring to this question , how can i get the current page size in

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.