C# Programmatically make enum












-1















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?










share|improve this question


















  • 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 '18 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 '18 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 '18 at 2:21






  • 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 '18 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 '18 at 2:42
















-1















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?










share|improve this question


















  • 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 '18 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 '18 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 '18 at 2:21






  • 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 '18 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 '18 at 2:42














-1












-1








-1








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 2:14









ArphileArphile

44412




44412








  • 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 '18 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 '18 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 '18 at 2:21






  • 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 '18 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 '18 at 2:42














  • 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 '18 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 '18 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 '18 at 2:21






  • 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 2:42












1 Answer
1






active

oldest

votes


















4














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)





share|improve this answer



















  • 4





    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 '18 at 3:08











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
});


}
});














draft saved

draft discarded


















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









4














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)





share|improve this answer



















  • 4





    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 '18 at 3:08
















4














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)





share|improve this answer



















  • 4





    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 '18 at 3:08














4












4








4







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)





share|improve this answer













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)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 2:48









bcwhimsbcwhims

707410




707410








  • 4





    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 '18 at 3:08














  • 4





    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 '18 at 3:08








4




4





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 '18 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 '18 at 3:08


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]