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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:19:16+00:00 2026-06-18T11:19:16+00:00

I have a class, BaseClass , that has a GetPropertyByDataMemberName method. This method uses

  • 0

I have a class, BaseClass, that has a GetPropertyByDataMemberName method. This method uses reflection to get the property that has a DataMemberAttribute with a specified name. I want to define it as a static method in BaseClass so I don’t have to define it in each subclass.

Problem is, since this method uses reflection to look up the properties, I need to get the current Type somehow in order to call GetProperties. Since it’s a static method, I can’t call GetType, so I’m at a loss of how to do that!

abstract class BaseClass
{
    [DataMember(Name = "p1")]
    public int PropertyOne{ get; set; }

    public static PropertyInfo GetPropertyByDataMemberName(string dataMemberName)
    {
        return GetType() // argh! can't call this statically!
            .GetProperties()
            .Where(z => Attribute.IsDefined(z, typeof(DataMemberAttribute)))
            .Single(z => ((DataMemberAttribute)Attribute.GetCustomAttribute(z, typeof(DataMemberAttribute))).Name == dataMemberName);
    }
}

Why not make it non-static? Well, let’s say I have a subclass like this:

class SubClassOne : BaseClass
{
    [DataMember(Name = "p2")]
    public string PropertyTwo { get; set; }
}

I want to be able to do something like this:

static void Main(string[] args)
{
    // print property names
    Console.WriteLine(BaseClass.GetPropertyByDataMemberName("p1").Name);   // should work
    Console.WriteLine(BaseClass.GetPropertyByDataMemberName("p2").Name);   // should not work
    Console.WriteLine(SubClassOne.GetPropertyByDataMemberName("p1").Name); // should work
    Console.WriteLine(SubClassOne.GetPropertyByDataMemberName("p2").Name); // should work
}

I’ve tried making GetPropertyByDataMemberName use typeof(BaseClass), but that only gets the properties of BaseClass, not any of the subclasses.

public static PropertyInfo GetPropertyByDataMemberName(string dataMemberName)
{
    return typeof(BaseClass)
        .GetProperties() // only gets properties of BaseClass
        .Where(z => Attribute.IsDefined(z, typeof(DataMemberAttribute)))
        .Single(z => ((DataMemberAttribute)Attribute.GetCustomAttribute(z, typeof(DataMemberAttribute))).Name == dataMemberName);
}

So, how to do this?

  • 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-06-18T11:19:17+00:00Added an answer on June 18, 2026 at 11:19 am

    You can use generics to achieve this:

    public static PropertyInfo GetPropertyByDataMemberName<T>(string dataMemberName)
        where T : BaseClass
    {
        return typeof(T)
            .GetProperties()
            .Where(z => Attribute.IsDefined(z, typeof(DataMemberAttribute)))
            .Single(z => ((DataMemberAttribute)Attribute.GetCustomAttribute(z, typeof(DataMemberAttribute))).Name == dataMemberName);
    }
    
    // Shortcut overload for properties on BaseClass.
    public static PropertyInfo GetPropertyByDataMemberName(string dataMemberName)
    {
        return GetPropertyByDataMemberName<BaseClass>(dataMemberName);
    }
    

    You would call it like this:

    static void Main(string[] args)
    {
        Console.WriteLine(BaseClass.GetPropertyByDataMemberName("p1").Name);   // should work
        Console.WriteLine(BaseClass.GetPropertyByDataMemberName("p2").Name);   // should not work
        Console.WriteLine(BaseClass.GetPropertyByDataMemberName<SubClassOne>("p1").Name); // should work
        Console.WriteLine(BaseClass.GetPropertyByDataMemberName<SubClassOne>("p2").Name); // should work
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class that has a static member. it is also a base
I have a base class that has the following property: public virtual Dictionary<String, int>
I have a base class that has an abstract method which returns a list
So I have a base class that has many children. This base class defines
i have a class that has a ConcurrentDictionary as a private member. This class
I have a class that inherits from a base class (this contains a lot
I have a virtual class that has been derived from a non virtual class.
I have a base controller class: And all my other controller inherits this BaseClass
I have a base class which has an embedded List that can be used
I have a base class that has methods that use a generic type 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.