C# Programmatically make enum
I'd like ask is there any method to make enum with string array.
for example, usually we make enum like this.
public enum Temp {
aa = 0,
bb,
cc,
dd
}
What I wish to know is, how to convert String array to enum programmatically.
String Temp = { aa, bb, cc, dd }
to above one ( String to enum ).
is it possible?
if then, how can I do this?
c# .net enums
|
show 2 more comments
I'd like ask is there any method to make enum with string array.
for example, usually we make enum like this.
public enum Temp {
aa = 0,
bb,
cc,
dd
}
What I wish to know is, how to convert String array to enum programmatically.
String Temp = { aa, bb, cc, dd }
to above one ( String to enum ).
is it possible?
if then, how can I do this?
c# .net enums
1
Please be very specific, Do you want to convert a string array to anenum
array. or do you want create a newenum
based on a string array? In the case of the later, it doesnt make any sense what-so-ever. how would you even write further code for it. What i am guessing you want, is maybe aDictionary<string,int>
– TheGeneral
Nov 21 at 2:17
3
You can’t create an enum programmatically because an enum is created at compile time and your program doesn’t run until run time.
– John Wu
Nov 21 at 2:19
3
This feels like a XY Problem - meta.stackexchange.com/questions/66377/what-is-the-xy-problem . Why do you want to do this?
– mjwills
Nov 21 at 2:21
1
Dictionary<string,int>
most likely, depending on your needs, example usagevar myInt = myDict["aa"]
orif(someValue == myDict[someKeyFormArray])
ect ect
– TheGeneral
Nov 21 at 2:24
1
Yes you can programmatically do this. You will have to look into System.CodDom theres a bunch of material that has to be covered so I didn't write a answer to the question.
– Levon Ravel
Nov 21 at 2:42
|
show 2 more comments
I'd like ask is there any method to make enum with string array.
for example, usually we make enum like this.
public enum Temp {
aa = 0,
bb,
cc,
dd
}
What I wish to know is, how to convert String array to enum programmatically.
String Temp = { aa, bb, cc, dd }
to above one ( String to enum ).
is it possible?
if then, how can I do this?
c# .net enums
I'd like ask is there any method to make enum with string array.
for example, usually we make enum like this.
public enum Temp {
aa = 0,
bb,
cc,
dd
}
What I wish to know is, how to convert String array to enum programmatically.
String Temp = { aa, bb, cc, dd }
to above one ( String to enum ).
is it possible?
if then, how can I do this?
c# .net enums
c# .net enums
asked Nov 21 at 2:14
Arphile
40512
40512
1
Please be very specific, Do you want to convert a string array to anenum
array. or do you want create a newenum
based on a string array? In the case of the later, it doesnt make any sense what-so-ever. how would you even write further code for it. What i am guessing you want, is maybe aDictionary<string,int>
– TheGeneral
Nov 21 at 2:17
3
You can’t create an enum programmatically because an enum is created at compile time and your program doesn’t run until run time.
– John Wu
Nov 21 at 2:19
3
This feels like a XY Problem - meta.stackexchange.com/questions/66377/what-is-the-xy-problem . Why do you want to do this?
– mjwills
Nov 21 at 2:21
1
Dictionary<string,int>
most likely, depending on your needs, example usagevar myInt = myDict["aa"]
orif(someValue == myDict[someKeyFormArray])
ect ect
– TheGeneral
Nov 21 at 2:24
1
Yes you can programmatically do this. You will have to look into System.CodDom theres a bunch of material that has to be covered so I didn't write a answer to the question.
– Levon Ravel
Nov 21 at 2:42
|
show 2 more comments
1
Please be very specific, Do you want to convert a string array to anenum
array. or do you want create a newenum
based on a string array? In the case of the later, it doesnt make any sense what-so-ever. how would you even write further code for it. What i am guessing you want, is maybe aDictionary<string,int>
– TheGeneral
Nov 21 at 2:17
3
You can’t create an enum programmatically because an enum is created at compile time and your program doesn’t run until run time.
– John Wu
Nov 21 at 2:19
3
This feels like a XY Problem - meta.stackexchange.com/questions/66377/what-is-the-xy-problem . Why do you want to do this?
– mjwills
Nov 21 at 2:21
1
Dictionary<string,int>
most likely, depending on your needs, example usagevar myInt = myDict["aa"]
orif(someValue == myDict[someKeyFormArray])
ect ect
– TheGeneral
Nov 21 at 2:24
1
Yes you can programmatically do this. You will have to look into System.CodDom theres a bunch of material that has to be covered so I didn't write a answer to the question.
– Levon Ravel
Nov 21 at 2:42
1
1
Please be very specific, Do you want to convert a string array to an
enum
array. or do you want create a new enum
based on a string array? In the case of the later, it doesnt make any sense what-so-ever. how would you even write further code for it. What i am guessing you want, is maybe a Dictionary<string,int>
– TheGeneral
Nov 21 at 2:17
Please be very specific, Do you want to convert a string array to an
enum
array. or do you want create a new enum
based on a string array? In the case of the later, it doesnt make any sense what-so-ever. how would you even write further code for it. What i am guessing you want, is maybe a Dictionary<string,int>
– TheGeneral
Nov 21 at 2:17
3
3
You can’t create an enum programmatically because an enum is created at compile time and your program doesn’t run until run time.
– John Wu
Nov 21 at 2:19
You can’t create an enum programmatically because an enum is created at compile time and your program doesn’t run until run time.
– John Wu
Nov 21 at 2:19
3
3
This feels like a XY Problem - meta.stackexchange.com/questions/66377/what-is-the-xy-problem . Why do you want to do this?
– mjwills
Nov 21 at 2:21
This feels like a XY Problem - meta.stackexchange.com/questions/66377/what-is-the-xy-problem . Why do you want to do this?
– mjwills
Nov 21 at 2:21
1
1
Dictionary<string,int>
most likely, depending on your needs, example usage var myInt = myDict["aa"]
or if(someValue == myDict[someKeyFormArray])
ect ect– TheGeneral
Nov 21 at 2:24
Dictionary<string,int>
most likely, depending on your needs, example usage var myInt = myDict["aa"]
or if(someValue == myDict[someKeyFormArray])
ect ect– TheGeneral
Nov 21 at 2:24
1
1
Yes you can programmatically do this. You will have to look into System.CodDom theres a bunch of material that has to be covered so I didn't write a answer to the question.
– Levon Ravel
Nov 21 at 2:42
Yes you can programmatically do this. You will have to look into System.CodDom theres a bunch of material that has to be covered so I didn't write a answer to the question.
– Levon Ravel
Nov 21 at 2:42
|
show 2 more comments
1 Answer
1
active
oldest
votes
I would be curious what you would want to do with this, but you can create a new enum type at runtime.
private static Type BuildEnum(string enumValues, string enumName)
{
AssemblyName aName = new AssemblyName("TempAssembly");
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName, AssemblyBuilderAccess.RunAndSave);
var eb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll").DefineEnum(enumName, TypeAttributes.Public, typeof(int));
for (int i = 0; i < enumValues.Length; i++)
{
eb.DefineLiteral(enumValues[i], i);
}
return eb.CreateType();
}
var myEnum = BuildEnum(new string { "aa", "bb", "cc", "dd" }, "Temp");
var enumValues = Enum.GetValues(myEnum)
3
This is clever, but I don't understand the value of doing this at run-time in the first place. I'd like the OP to explain why this is important to them.
– Enigmativity
Nov 21 at 3:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404402%2fc-sharp-programmatically-make-enum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would be curious what you would want to do with this, but you can create a new enum type at runtime.
private static Type BuildEnum(string enumValues, string enumName)
{
AssemblyName aName = new AssemblyName("TempAssembly");
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName, AssemblyBuilderAccess.RunAndSave);
var eb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll").DefineEnum(enumName, TypeAttributes.Public, typeof(int));
for (int i = 0; i < enumValues.Length; i++)
{
eb.DefineLiteral(enumValues[i], i);
}
return eb.CreateType();
}
var myEnum = BuildEnum(new string { "aa", "bb", "cc", "dd" }, "Temp");
var enumValues = Enum.GetValues(myEnum)
3
This is clever, but I don't understand the value of doing this at run-time in the first place. I'd like the OP to explain why this is important to them.
– Enigmativity
Nov 21 at 3:08
add a comment |
I would be curious what you would want to do with this, but you can create a new enum type at runtime.
private static Type BuildEnum(string enumValues, string enumName)
{
AssemblyName aName = new AssemblyName("TempAssembly");
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName, AssemblyBuilderAccess.RunAndSave);
var eb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll").DefineEnum(enumName, TypeAttributes.Public, typeof(int));
for (int i = 0; i < enumValues.Length; i++)
{
eb.DefineLiteral(enumValues[i], i);
}
return eb.CreateType();
}
var myEnum = BuildEnum(new string { "aa", "bb", "cc", "dd" }, "Temp");
var enumValues = Enum.GetValues(myEnum)
3
This is clever, but I don't understand the value of doing this at run-time in the first place. I'd like the OP to explain why this is important to them.
– Enigmativity
Nov 21 at 3:08
add a comment |
I would be curious what you would want to do with this, but you can create a new enum type at runtime.
private static Type BuildEnum(string enumValues, string enumName)
{
AssemblyName aName = new AssemblyName("TempAssembly");
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName, AssemblyBuilderAccess.RunAndSave);
var eb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll").DefineEnum(enumName, TypeAttributes.Public, typeof(int));
for (int i = 0; i < enumValues.Length; i++)
{
eb.DefineLiteral(enumValues[i], i);
}
return eb.CreateType();
}
var myEnum = BuildEnum(new string { "aa", "bb", "cc", "dd" }, "Temp");
var enumValues = Enum.GetValues(myEnum)
I would be curious what you would want to do with this, but you can create a new enum type at runtime.
private static Type BuildEnum(string enumValues, string enumName)
{
AssemblyName aName = new AssemblyName("TempAssembly");
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName, AssemblyBuilderAccess.RunAndSave);
var eb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll").DefineEnum(enumName, TypeAttributes.Public, typeof(int));
for (int i = 0; i < enumValues.Length; i++)
{
eb.DefineLiteral(enumValues[i], i);
}
return eb.CreateType();
}
var myEnum = BuildEnum(new string { "aa", "bb", "cc", "dd" }, "Temp");
var enumValues = Enum.GetValues(myEnum)
answered Nov 21 at 2:48
bcwhims
626410
626410
3
This is clever, but I don't understand the value of doing this at run-time in the first place. I'd like the OP to explain why this is important to them.
– Enigmativity
Nov 21 at 3:08
add a comment |
3
This is clever, but I don't understand the value of doing this at run-time in the first place. I'd like the OP to explain why this is important to them.
– Enigmativity
Nov 21 at 3:08
3
3
This is clever, but I don't understand the value of doing this at run-time in the first place. I'd like the OP to explain why this is important to them.
– Enigmativity
Nov 21 at 3:08
This is clever, but I don't understand the value of doing this at run-time in the first place. I'd like the OP to explain why this is important to them.
– Enigmativity
Nov 21 at 3:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404402%2fc-sharp-programmatically-make-enum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please be very specific, Do you want to convert a string array to an
enum
array. or do you want create a newenum
based on a string array? In the case of the later, it doesnt make any sense what-so-ever. how would you even write further code for it. What i am guessing you want, is maybe aDictionary<string,int>
– TheGeneral
Nov 21 at 2:17
3
You can’t create an enum programmatically because an enum is created at compile time and your program doesn’t run until run time.
– John Wu
Nov 21 at 2:19
3
This feels like a XY Problem - meta.stackexchange.com/questions/66377/what-is-the-xy-problem . Why do you want to do this?
– mjwills
Nov 21 at 2:21
1
Dictionary<string,int>
most likely, depending on your needs, example usagevar myInt = myDict["aa"]
orif(someValue == myDict[someKeyFormArray])
ect ect– TheGeneral
Nov 21 at 2:24
1
Yes you can programmatically do this. You will have to look into System.CodDom theres a bunch of material that has to be covered so I didn't write a answer to the question.
– Levon Ravel
Nov 21 at 2:42