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

  • Home
  • SEARCH
  • 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 7827939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:06:14+00:00 2026-06-02T10:06:14+00:00

This question is related to this one , but is not a duplicate. Jb

  • 0

This question is related to this one, but is not a duplicate. Jb posted there that to add a custom attribute, the following snippet would work:

ModuleDefinition module = ...;
MethodDefinition targetMethod = ...;
MethodReference attributeConstructor = module.Import(
    typeof(DebuggerHiddenAttribute).GetConstructor(Type.EmptyTypes));

targetMethod.CustomAttributes.Add(new CustomAttribute(attributeConstructor));
module.Write(...);

I would like to use something similar, but to add a custom attribute whose constructor takes two string parameters in its (only) constructor, and I’d like to specify values for those (obviously). Can anyone help?

  • 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-02T10:06:16+00:00Added an answer on June 2, 2026 at 10:06 am

    First you have to get a reference to the proper version of the constructor:

    MethodReference attributeConstructor = module.Import(
        typeof(MyAttribute).GetConstructor(new [] { typeof(string), typeof(string) }));
    

    Then you can simply populate the custom attributes with string arguments:

    CustomAttribute attribute = new CustomAttribute(attributeConstructor);
    attribute.ConstructorArguments.Add(
            new CustomAttributeArgument(
                module.TypeSystem.String, "Foo"));
    attribute.ConstructorArguments.Add(
            new CustomAttributeArgument(
                module.TypeSystem.String, "Bar"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(This question is related - but not the same - to this one) This
[This question is related to but not the same as this one .] My
[This question is related to but not the same as this one .] If
Note: this question is related to this one , but two years is a
This is related to my previous question , but a different one. I have
I've look for this, and there's some related questions, but no one gives me
This question is related with one of my earlier questions.. Previous Post In there
Another question related to this one . I have a List<SortableObjects> that is the
This is a related question to one I posted earlier... I'm trying to sum
This is a related question to one I posted earlier today, I was initially

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.