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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:12:34+00:00 2026-05-15T08:12:34+00:00

What is the syntax for defining an array literal in CIL for the purposes

  • 0

What is the syntax for defining an array literal in CIL for the purposes of decorating a member with a custom attribute?

I am writing some .NET code in CIL (using ilasm.exe to compile it) and I need to decorate a method with a custom attribute. The constructor for that attribute takes an array of integers as its only parameter. How can I do this in CIL?

This is the signature of the custom attribute’s constructor (I can’t change it):

public FooAttribute(int[] values) {
// some hidden constructor stuff
}

This is how I’d decorate my method if I were writing in C# (but I can’t):


[Foo(new int[] {1, 2, 3, 4})]
public string Bar() {
  return "Some text";
}

Using ildasm.exe to look at the compiled C# (to try and understand by reverse engineering) gives me an ugly and unusable binary literal. I tried using Reflector.NET instead and it looks much better but ilasm.exe throws a syntax error at the keyword “new” so I can’t use it:

.custom instance void SomeNamespace.FooAttribute::.ctor(int32[]) = { new int32[int32(4)] { int32(1), int32(2), int32(3), int32(4) } }
  • 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-15T08:12:35+00:00Added an answer on May 15, 2026 at 8:12 am

    Hard to guess what your problem might be. If I apply this attribute to a Program.Test() method, I get this:

      .method private hidebysig static void  Test() cil managed
      {
        .custom instance void ConsoleApplication1.FooAttribute::.ctor(int32[]) = ( 01 00 04 00 00 00 01 00 00 00 02 00 00 00 03 00 
                                                                                   00 00 04 00 00 00 00 00 ) 
        // Code size       2 (0x2)
        .maxstack  8
        IL_0000:  nop
        IL_0001:  ret
      } // end of method Program::Test
    

    Run this through ilasm.exe, no problem. Note how the array element values (scroll the snippet window to the right to see them) are already converted into the format needed to embed them in the attribute constructor data table. BitConverter.GetBytes() can get part of that job done. The Ecma document should have the required format of that data.

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

Sidebar

Related Questions

No related questions found

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.