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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:43:36+00:00 2026-06-11T09:43:36+00:00

Is there a way I can apply an attribute to a struct conditionally? If

  • 0

Is there a way I can apply an attribute to a struct conditionally?

If the machine is 32bit I want to apply this attribute

[StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Unicode)]

If the machine is 64bit I want to apply this attribute

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

Or alternatively could I substitute a value within the attribute…

32bit (Pack = 2)

[StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Unicode)]

64bit (Pack = 8)

[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Unicode)]

I tried using this example but it’s for custom attributes, not existing ones.

Update:

  • I’d like to comile to “Any CPU”
  • The attribute is for the SHFILEOPSTRUCT and depending on the processor uses either or.
  • I don’t want to have to compile two versions.
  • 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-11T09:43:38+00:00Added an answer on June 11, 2026 at 9:43 am

    Good question.

    The answer I first thought of was preprocessor directives and 32- and 64-bit compiled assemblies. You can use the same code, even the same project, just build and deploy it two ways depending on the target system:

    #ifdef Bit32
    [StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Unicode)]
    #endif
    #ifdef Bit64
    [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Unicode)]
    #endif
    

    This would require defining Bit32 and Bit64 compilation constants for your project based on the target architecture, and probably building your app twice.

    If you want to do this at runtime, I don’t think it’s possible unless you emit the entire class dynamically at runtime. The attributes may only have constant data, and they cannot be conditionally applied at runtime (preprocessor directives operate at compile-time, not runtime).

    The only other way I can think to do this is to copy the class definition into two namespaces, and conditionally use one or the other based on the Environment.Is64BitOperatingSystem property. You can use this property to conditionally control which class you instantiate, or which strategy for creation you choose (which factory method or related pattern is used), but you can’t conditionally control attributes at runtime; their information is statically compiled into the assembly manifest as metadata. This one in particular is used by the runtime itself to define how it stores the object’s members as heap data, and you don’t ever really look for this attribute in user code and use it to define behavior (thus ignoring or specifying a conditional Pack value at runtime).

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

Sidebar

Related Questions

How can you do this with jQuery, in an elegant way? Apply z attribute
Is there a way I can apply '+ to '( 1 2 3)? edit:
Is there a way I can apply the login_required decorator to an entire app?
I would like to know if there is any way I can apply 100%
Is there any way to apply an attribute to a model file in ASP.NET
Is there any way can declare a bean in just like JSP UseBean in
Is there way that I can read the file from remote server using fopen
Is there any way I can set a formatter on models that will convert
Is there any way we can fetch X509 Public Cetrificates using c# from AD
Is there a way I can use one category to stylize all of my

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.