Access managed C++ public enum class from C# by Dynamically loading
-
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.
-
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.
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 constantDon't forget that without
enum class
orenum 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
-
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 constantDon't forget that without
enum class
orenum 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
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