Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Access managed C++ public enum class from C# by Dynamically loading

Access managed C++ public enum class from C# by Dynamically loading

Scheduled Pinned Locked Moved C#
csharpc++
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mjvalan
    wrote on last edited by
    #1

    I have an managed C++ dll lib. It has claas info like below namespace tests { public enum class clours { Start = 0; Red = 1; Green = 2; Blue = 3; End = 4; } } I have written a client application in C# and load the dll dynamically(I am using reflection). I want to access the enum members and values between 'Start' and 'End' from the client application.

    E 1 Reply Last reply
    0
    • M mjvalan

      I have an managed C++ dll lib. It has claas info like below namespace tests { public enum class clours { Start = 0; Red = 1; Green = 2; Blue = 3; End = 4; } } I have written a client application in C# and load the dll dynamically(I am using reflection). I want to access the enum members and values between 'Start' and 'End' from the client application.

      E Offline
      E Offline
      Eslam Afifi
      wrote on last edited by
      #2

      var fields = (from field in type.GetFields() // type is the enum's type (A System.Type)
      where (field.Attributes & System.Reflection.FieldAttributes.Literal) != 0
      select field).ToArray();

      // fields[0].GetRawConstantValue() gets the value of the enum's first constant
      // fields[0].Name gets the name of the enum's first constant

      Don't forget that without enum class or enum struct this code won't work. The criteria in the where clause assumes that for a CLR enum, no fields other than the enum's constants marked as literal. I'm not completely sure if there are no special cases but you should research that a little bit before trusting that criteria.

      Eslam Afifi

      H 1 Reply Last reply
      0
      • E Eslam Afifi

        var fields = (from field in type.GetFields() // type is the enum's type (A System.Type)
        where (field.Attributes & System.Reflection.FieldAttributes.Literal) != 0
        select field).ToArray();

        // fields[0].GetRawConstantValue() gets the value of the enum's first constant
        // fields[0].Name gets the name of the enum's first constant

        Don't forget that without enum class or enum struct this code won't work. The criteria in the where clause assumes that for a CLR enum, no fields other than the enum's constants marked as literal. I'm not completely sure if there are no special cases but you should research that a little bit before trusting that criteria.

        Eslam Afifi

        H Offline
        H Offline
        Hossam Ahmed
        wrote on last edited by
        #3

        GetRawConstantValue() Method is not supported in .NET CF 2.0 anyone have any idea about how to get the value of enum constant? Enum.GetNames() is not supported in .NET CF 2.0, but i could getting the name of enum constants. Thanks. --- Hossam Ahmed

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups