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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:02:50+00:00 2026-05-25T13:02:50+00:00

I have done a bit with C# method attributes but I have not done

  • 0

I have done a bit with C# method attributes but I have not done something that covers this requirement, and I am not even sure it is possible but here goes.

Let say I have a class like..

public class MyObject
{
   public DateTime? TheTime {get;set;}
   public string AValue {get;set;}
}

And a function like this…

public void AddObject(MyObject mo)
{
   //Do Something
}

Now, when the object is passed AValue will be set, but TheTime will be null (as this needs to be set in the AddObject method). I can do this like…

public void AddObject(MyObject mo)
{
   mo.TheTime = DateTime.Now;
   //Do Something
}

But I don’t like that, what I want is to create an attribute that will do the job for me. So ultimately, I want something like this

[AutoUpdate(Parameter = "mo")]
public void AddObject(MyObject mo)
{
   //Do Something
}

and the magic is done for me.

Any ideas on how to create an attribute to do this? A simple link would do, I can’t seem to find what I am looking for at the minute

  • 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-25T13:02:50+00:00Added an answer on May 25, 2026 at 1:02 pm

    You can achieve this with a cross-cutting concerns library (AOP), which applies a decorator pattern around your method, allowing you to inject functionality via attributes. It’s usually used for things like logging.

    Take a look at Spring.Net or Castle Windsor.

    A a quick example of decorator pattern to create your own implementation.

    public class MyExtendedClass : IMyClass
    {
        public MyExtendedClass(IMyClass myClass)
        {
            this.innerMyClass= myClass;
        }
    
        public void AddObject(MyObject mo)
        {
           // does mo have an attribute on AddObject
           // if so
           Type clsType = innerMyClass.GetType();
           MethodInfo mInfo = clsType.GetMethod("AddObject");
           if (Attribute.IsDefined(mInfo, typeof(AutoUpdateAttribute)))
           {
              mo.TheTime = DateTime.Now;
           }
           innerMyClass.AddObject(mo);
        } 
    }
    

    Then use like this:

    IMyClass myClass = new MyExtendedClass(new MyClass());
    myClass.AddObject(mo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done a bit of research into this and it seems that the
Okay, I have done a bit of searching online and found this thread, but
I've done quite a bit of googling on this error but have had no
I'm sure I have done this before in the past, but I've been in
I have done a bit of testing on this myself (During the server side
I've done quite a bit of programming on Windows but now I have to
I've done a fair bit of UDP socket programming in the past, but have
Let me clarify this a bit... I have a class that handles XML files.
I am wondering if something like this could be done(of course what I have
I have some code (that is working), but I just want to make sure

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.