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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:57:17+00:00 2026-05-28T01:57:17+00:00

I have a class that is declared Internal. It is decorated with various annotations.

  • 0

I have a class that is declared Internal. It is decorated with various annotations. In particular is the [DisplayName(“My Display Name”)] annotation. I have some code that will retrieve the value but only works if the class is declared public. I am sort of new to using reflection. I believe I need to specify that the BindingFlags.NonPublic be used but I am not sure where.

LinqPAD code:

void Main()
{
    List<SpGetProfileInfoResult> p = new List<SpGetProfileInfoResult>();
    p.Add(new SpGetProfileInfoResult() { FName = "Eric" });
    p.Add(new SpGetProfileInfoResult() { FName = "Mike" });

    p.Dump();

    foreach (var item in p)
    {
        Console.WriteLine(item.DisplayName(i => i.FName));
        Console.WriteLine(item.FName);
    }

}

public partial class SpGetProfileInfoResult
{
    // Uncomment this annotation to see that this part will work
    // [System.ComponentModel.DisplayNameAttribute("[BILLTO-FNAME]")]
    public string FName { get; set; }
}

public partial class SpGetProfileInfoResult
{
    internal class Metadata
    {
        // This attribute is never available seems.
        [System.ComponentModel.DisplayNameAttribute("[BILL-FNAME]")]
        public string FName { get; set; }
    }
}

public static class Tag
{
    public static T GetAttribute<T>(this MemberInfo member, bool isRequired) where T : Attribute
    {
        var attribute = member.GetCustomAttributes(typeof(T), false).SingleOrDefault();

        if (attribute == null && isRequired)
        {
            throw new ArgumentException(
                string.Format(
                "The {0} attribute must be defined on member {1}",
                typeof(T).Name,
                member.Name));
        }

        return (T)attribute;
    }

    public static string DisplayName<T>(this T src,Expression<Func<T, object>> propertyExpression)
    {
        Type metadata = null;

        var memberInfo = GetPropertyInformation(propertyExpression.Body);
        if (memberInfo == null)
        {
            throw new ArgumentException(
                "No property reference expression was found.",
                "propertyExpression");
        }

        var attr = memberInfo.GetAttribute<DisplayNameAttribute>(false);
        if (attr == null)
        {
            return memberInfo.Name;
        }

        return attr.DisplayName;
    }

    public static MemberInfo GetPropertyInformation(Expression propertyExpression)
    {
        MemberExpression memberExpr = propertyExpression as MemberExpression;
        if (memberExpr == null)
        {
            UnaryExpression unaryExpr = propertyExpression as UnaryExpression;
            if (unaryExpr != null && unaryExpr.NodeType == ExpressionType.Convert)
            {
                memberExpr = unaryExpr.Operand as MemberExpression;
            }
        }

        if (memberExpr != null && memberExpr.Member.MemberType == MemberTypes.Property)
        {
            return memberExpr.Member;
        }

        return null;
    }
}

Usage:

If you don’t have LinqPAD, you should download it then you can test this pretty easily by just creating a new C# Program in LinkPAD

Debug.WriteLine(item.DisplayName(i => i.FName));
  • 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-28T01:57:18+00:00Added an answer on May 28, 2026 at 1:57 am

    So it looks like you want to be able to decorate existing members of a partial class, by providing metadata in a separate partial piece. There’s no built-in mechanism for that (see eg this question and the classes mentioned in the answer), but if you’re willing to stick to a convention, you can roll your own:

    So suppose we have

    public partial class SpGetProfileInfoResult
    {
        public string FName { get; set; }
    }
    

    in a partial piece we can’t change, and

    public partial class SpGetProfileInfoResult
    {
        internal class Metadata
        {
            [System.ComponentModel.DisplayNameAttribute("[BILL-FNAME]")]
            public string FName { get; set; }
        }
    }
    

    in a partial piece we can change. You already have most of the pieces: in DisplayName(), you successfully determine that we are looking at the FName property; you then look for a DisplayNameAttribute on T.FName, but there isn’t one, so that’s where it stops.

    What you need to do is, in the case where you don’t find the attribute you need,

    var attr = memberInfo.GetAttribute<DisplayNameAttribute>(false);
    if (attr == null)
    {
    

    Look for a nested class named Metadata – note here is one place we use BindingFlags.NonPublic

        // Try and get a nested metadata class
        var metadataType = typeof(T)
            .GetNestedType("Metadata", 
                           BindingFlags.Public | BindingFlags.NonPublic);
    

    If we find one:

        if (metadataType != null)
        {
    

    Look for a member of the same name as was originally being talked about (BindingFlags.NonPublic again)

            var membersOnMetadataType = metadataType.GetMember(memberInfo.Name, 
                BindingFlags.Instance |
                BindingFlags.Public | 
                BindingFlags.NonPublic);
    

    If there is one, use your helper method, but this time pass it the metadata type’s member:

            if (membersOnMetadataType.Any())
            {
                var attrOnMetadataType = membersOnMetadataType[0]
                    .GetAttribute<DisplayNameAttribute>(false);
                return attrOnMetadataType.DisplayName;
    

    (I’ve omitted a final nullity check here, as well as closing the control flow)

    Depending on how distasteful you find that "Metadata" string, you could instead do something declarative with attributes:

    • have a class-level attribute that goes on SpGetProfileInfoResult (the piece you can change) that points at its Metadata using typeof (this is the approach taken by System.ComponentModel), or
    • have a class-level attribute that goes on Metadata, to have it claim ‘I am a metadata type’. Then instead of searching for a nested class named a fixed string, we would instead search for a nested class having this particular attribute.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a c# static class that I have declared as internal. I also
We have some code that looks like this: class Serializer { public: template<class Type>
I have a class that offers up a few events. That class is declared
I have a class that works by summing NSNumbers. One particular use of this
//class = Person public string Name { get; internal set; } I have an
I have got class declared like this: internal private abstract class BoxGroup<TS> : IBoxGroup
Is it possible to have Code First data classes declared with internal access as
Breaking down the MS RBTree (an internal .Net abstract class), I have discovered that
I have a very basic data class that is subclassed from NSObject. I declare
I have class that represents users. Users are divided into two groups with different

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.