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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:47:28+00:00 2026-06-13T09:47:28+00:00

I want to pass in the attribute name and return the value. This will

  • 0

I want to pass in the attribute name and return the value. This will be in a generic util and it will not know the attribute type.

Update
This is the actual working code if someone needs to do this. I needed a way to have the core code parse the attributes without knowing what they were.

public void LoadPropertiesToGrid(BaseGridPropertyModel model)
{
    foreach (PropertyInfo prop in ReflectionUtil.FindPublicPropeties(model))
    {
        object editTyp = ReflectionUtil.GetPropertyAttributes(prop, "EditorType"); 
        object rowIdx = ReflectionUtil.GetPropertyAttributes(prop, "ColIndex");
        object name = ReflectionUtil.GetPropertyAttributes(prop, "Name");
        object visible = ReflectionUtil.GetPropertyAttributes(prop, "Visible");
        ConfigureColumn((string) name, (int) rowIdx, (bool) visible, (string) editTyp);
     }
}
[Serializable]
public class CanvasPropertiesViewModel : BaseGridPropertyModel
{
    [PropertiesGrid(Name = "TEsting Name 0", ColIndex = 0)]
    public string StringData1 { get; set; }

    [PropertiesGrid(Name = "TEsting Name 2", ColIndex = 2)]
    public string StringData2 { get; set; }

    [PropertiesGrid(Name = "TEsting Name 1", ColIndex = 1)]
    public string StringData3 { get; set; }
}
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class PropertiesGridAttribute : Attribute
{
    /// <summary>
    /// Editor type
    /// </summary>
    public Type EditorType { get; set; }

    /// <summary>
    /// Sets Column Index
    /// </summary>
    public int ColIndex { get; set; }

    /// <summary>
    /// Visible to Grid
    /// </summary>
    public bool Visible { get; set; }

    /// <summary>
    /// Dispaly Name of the property
    /// </summary>
    public string Name { get; set; }
}
public static object GetPropertyAttributes(PropertyInfo prop, string attributeName)
{
    // look for an attribute that takes one constructor argument
    foreach (CustomAttributeData attribData in prop.GetCustomAttributesData())
    {
        string typeName = attribData.Constructor.DeclaringType.Name;
        //if (attribData.ConstructorArguments.Count == 1 && (typeName == attributeName || typeName == attributeName + "Attribute"))
        //{
        //    return attribData.ConstructorArguments[0].Value;
        //}
        foreach (CustomAttributeNamedArgument att in attribData.NamedArguments)
        {
            if(att.GetPropertyValue<string>("MemberName") == attributeName)
            {
                return att.TypedValue.Value;
            }
        }
    }
    return null;
}

//PropertyExpressionParser 
public static TRet GetPropertyValue<TRet>(this object obj, string propertyPathName)
{
    if (obj == null)
    {
        throw new ArgumentNullException("obj");
    }

    string[] parts = propertyPathName.Split('.');
    string path = propertyPathName;
    object root = obj;

    if (parts.Length > 1)
    {
        path = parts[parts.Length - 1];
        parts = parts.TakeWhile((p, i) => i < parts.Length - 1).ToArray();
        string path2 = String.Join(".", parts);
        root = obj.GetPropertyValue<object>(path2);
    }

    var sourceType = root.GetType();
    var value = (TRet)sourceType.GetProperty(path).GetValue(root, null);
    return value;
}
  • 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-13T09:47:29+00:00Added an answer on June 13, 2026 at 9:47 am

    If you mean “given an attribute that takes one parameter, give me that value”, for example:

    [DisplayName("abc")] <===== "abc"
    [Browsable(true)] <===== true
    

    then this is easiest in .NET 4.5, via the new CustomAttributeData API:

    using System.ComponentModel;
    using System.Reflection;
    
    public static class Program
    {
        static void Main()
        {
            PropertyInfo prop = typeof(Foo).GetProperty("Bar");
            var val = GetPropertyAttributes(prop, "DisplayName");
        }
        public static object GetPropertyAttributes(PropertyInfo prop, string attributeName)
        {
            // look for an attribute that takes one constructor argument
            foreach(CustomAttributeData attribData in prop.GetCustomAttributesData()) 
            {
                string typeName = attribData.Constructor.DeclaringType.Name;
                if(attribData.ConstructorArguments.Count == 1 &&
                    (typeName == attributeName || typeName == attributeName + "Attribute"))
                {
                    return attribData.ConstructorArguments[0].Value;
                }
            }
            return null;
        }
    }
    
    class Foo
    {
        [DisplayName("abc")]
        public string Bar { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass the name attribute of the fileds like this... rules: {
I want to pass an ImageIcon instance by value as we know that by
I want to read a Rootnode element attribute value ,but my code is not
I want to pass XML as a string in an XML attribute. <activity evt=&lt;FHS&gt;
I want to pass a default argument to an instance method using the value
I want to pass tuple to this function but i am getting error mytuple
I want to pass some fields (for example like name information) into a form.
I want pass a class-instace to a method of an other and be sure
My data is just number, I want pass it from VC1 to VC2 for
I want to pass '#' to the query string like ?page.aspx?someParam=1234#5 .

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.