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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:01:36+00:00 2026-06-07T07:01:36+00:00

How can I write reflection code that goes through my project and looks for

  • 0

How can I write reflection code that goes through my project and looks for classes with special attribute and able to collection 2 pieces of information: ClassName.PropertyName and string that is being passed into ResourceManager.GetValue inside the property. My code is as follows. Please help. Thanks

    [TestMethod]
    public void TestMethod1()
    {

        Dictionary<string, string> mydictionary = new Dictionary<string, string>();
        mydictionary = ParseAllClassesWithAttribute("MyAttribute");

        Assert.AreEqual(mydictionary["MyClass.FirstNameIsRequired"].ToString(), "First Name is Required");


    }

    private Dictionary<string, string> ParseAllClassesWithAttribute(string p)
    {
        Dictionary<string,string> dictionary = new Dictionary<string, string>();

        // use reflection to go through the class that is decorated by attribute MyAttribute
        // and is able to extract ClassName.PropertyName along with what is Inside
        // GetValue method parameter.
        // In the following I am artificially populating the dictionary object.

        dictionary.Add("MyClass.FirstNameIsRequired", "First Name is Required");
        return dictionary;
    }



     [MyAttribute]
     public class MyClass
        {
            public string FirstNameIsRequired
            {
                get
                {
                    return ResourceManager.GetValue("First Name is required");
                }
            }
        }

        public static class ResourceManager
        {
            public static string GetValue(string key)
            {
                return String.Format("some value from the database based on key {0}",key);
            }
        }
  • 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-07T07:01:38+00:00Added an answer on June 7, 2026 at 7:01 am

    Reflection can’t extract the "First Name is Required" value, unless you take the IL bytes and parse them. Here’s one potential solution:

    [AttributeUsage(AttributeTargets.Property)]
    public class MyAttribute : Attribute
    {
        public string TheString { get; private set; }
        public MyAttribute(string theString)
        {
            this.TheString = theString;
        }
    }
    
    const string FirstNameIsRequiredThing = "First Name is required";
    [MyAttribute(FirstNameIsRequiredThing)]
    public string FirstNameIsRequired
    {
        get
        {
            return ResourceManager.GetValue(FirstNameIsRequiredThing);
        }
    }
    

    Now you can read all properties with MyAttribute and see the expected values. (if you have trouble with this part, be sure to follow the advice of the comments on your question)

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

Sidebar

Related Questions

I'm trying write a code that can the set property value through the lambda
I can write my code in both the ways to achieve my objective, but
Can anyone please tell me how I can write a bash shell script that
Can someone please explain how you can write a url pattern and view that
I'm trying to write some code that will take any object and convert it
There's a piece of code that looks like this. The problem is that during
I'm attempting to write find/replace code for Word documents using Word Automation through Interop.Word
Is it possible in C# to write MSIL code that will add a preprocessor
How can I write a PostSharp aspect to apply an attribute to a class?
Using reflection, how can I get all types that implement an interface with C#

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.