.NET CORE List properties of an attribute












0















I have multiple classes that use this custom attribute. All of them implement the same interface and they all have this custom attribute and have different names



public class NameAttribute : Attribute
{
public NameAttribute(string name)
{
this.Name = name;
}

public string Name { get; }
}


Is there a way to return a list of these names in the controller?










share|improve this question

























  • List of values of that property? please explain what is meant by that?

    – Sujit.Warrier
    Nov 15 '18 at 8:42











  • In the controller i want to return a list of names from the attribute

    – kjwdamme
    Nov 15 '18 at 8:57













  • its string variable, so wither store the values as comma separated values the split and send

    – Sujit.Warrier
    Nov 15 '18 at 9:22











  • The problem is that i dont know how to access the name properties from the controller

    – kjwdamme
    Nov 15 '18 at 9:29
















0















I have multiple classes that use this custom attribute. All of them implement the same interface and they all have this custom attribute and have different names



public class NameAttribute : Attribute
{
public NameAttribute(string name)
{
this.Name = name;
}

public string Name { get; }
}


Is there a way to return a list of these names in the controller?










share|improve this question

























  • List of values of that property? please explain what is meant by that?

    – Sujit.Warrier
    Nov 15 '18 at 8:42











  • In the controller i want to return a list of names from the attribute

    – kjwdamme
    Nov 15 '18 at 8:57













  • its string variable, so wither store the values as comma separated values the split and send

    – Sujit.Warrier
    Nov 15 '18 at 9:22











  • The problem is that i dont know how to access the name properties from the controller

    – kjwdamme
    Nov 15 '18 at 9:29














0












0








0








I have multiple classes that use this custom attribute. All of them implement the same interface and they all have this custom attribute and have different names



public class NameAttribute : Attribute
{
public NameAttribute(string name)
{
this.Name = name;
}

public string Name { get; }
}


Is there a way to return a list of these names in the controller?










share|improve this question
















I have multiple classes that use this custom attribute. All of them implement the same interface and they all have this custom attribute and have different names



public class NameAttribute : Attribute
{
public NameAttribute(string name)
{
this.Name = name;
}

public string Name { get; }
}


Is there a way to return a list of these names in the controller?







asp.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 8:59







kjwdamme

















asked Nov 15 '18 at 8:30









kjwdammekjwdamme

486




486













  • List of values of that property? please explain what is meant by that?

    – Sujit.Warrier
    Nov 15 '18 at 8:42











  • In the controller i want to return a list of names from the attribute

    – kjwdamme
    Nov 15 '18 at 8:57













  • its string variable, so wither store the values as comma separated values the split and send

    – Sujit.Warrier
    Nov 15 '18 at 9:22











  • The problem is that i dont know how to access the name properties from the controller

    – kjwdamme
    Nov 15 '18 at 9:29



















  • List of values of that property? please explain what is meant by that?

    – Sujit.Warrier
    Nov 15 '18 at 8:42











  • In the controller i want to return a list of names from the attribute

    – kjwdamme
    Nov 15 '18 at 8:57













  • its string variable, so wither store the values as comma separated values the split and send

    – Sujit.Warrier
    Nov 15 '18 at 9:22











  • The problem is that i dont know how to access the name properties from the controller

    – kjwdamme
    Nov 15 '18 at 9:29

















List of values of that property? please explain what is meant by that?

– Sujit.Warrier
Nov 15 '18 at 8:42





List of values of that property? please explain what is meant by that?

– Sujit.Warrier
Nov 15 '18 at 8:42













In the controller i want to return a list of names from the attribute

– kjwdamme
Nov 15 '18 at 8:57







In the controller i want to return a list of names from the attribute

– kjwdamme
Nov 15 '18 at 8:57















its string variable, so wither store the values as comma separated values the split and send

– Sujit.Warrier
Nov 15 '18 at 9:22





its string variable, so wither store the values as comma separated values the split and send

– Sujit.Warrier
Nov 15 '18 at 9:22













The problem is that i dont know how to access the name properties from the controller

– kjwdamme
Nov 15 '18 at 9:29





The problem is that i dont know how to access the name properties from the controller

– kjwdamme
Nov 15 '18 at 9:29












1 Answer
1






active

oldest

votes


















0














I'll answer my own question for if someone has the same problem in the future. This is what I did:



        IEnumerable<Type> types = typeof(Startup).Assembly.ExportedTypes
.Where(type => type.IsClass && !type.IsAbstract)
.Where(type => type.GetInterface("IName") != null)
.Where(type => type.GetCustomAttribute<NameAttribute>() != null);

List<string> rules = new List<string>();

foreach (Type t in types)
{
rules.Add(t.GetCustomAttribute<NameAttribute>().Name);
}


Basicly what happens is that I look for all the exported types that implement an interface named "IName" and have the "NameAttribute" attribute. This will return a enumerable list with types.



Next it is possible to retrieve the custom attribute and put the name in an arraylist.






share|improve this answer























    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%2f53315197%2fnet-core-list-properties-of-an-attribute%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









    0














    I'll answer my own question for if someone has the same problem in the future. This is what I did:



            IEnumerable<Type> types = typeof(Startup).Assembly.ExportedTypes
    .Where(type => type.IsClass && !type.IsAbstract)
    .Where(type => type.GetInterface("IName") != null)
    .Where(type => type.GetCustomAttribute<NameAttribute>() != null);

    List<string> rules = new List<string>();

    foreach (Type t in types)
    {
    rules.Add(t.GetCustomAttribute<NameAttribute>().Name);
    }


    Basicly what happens is that I look for all the exported types that implement an interface named "IName" and have the "NameAttribute" attribute. This will return a enumerable list with types.



    Next it is possible to retrieve the custom attribute and put the name in an arraylist.






    share|improve this answer




























      0














      I'll answer my own question for if someone has the same problem in the future. This is what I did:



              IEnumerable<Type> types = typeof(Startup).Assembly.ExportedTypes
      .Where(type => type.IsClass && !type.IsAbstract)
      .Where(type => type.GetInterface("IName") != null)
      .Where(type => type.GetCustomAttribute<NameAttribute>() != null);

      List<string> rules = new List<string>();

      foreach (Type t in types)
      {
      rules.Add(t.GetCustomAttribute<NameAttribute>().Name);
      }


      Basicly what happens is that I look for all the exported types that implement an interface named "IName" and have the "NameAttribute" attribute. This will return a enumerable list with types.



      Next it is possible to retrieve the custom attribute and put the name in an arraylist.






      share|improve this answer


























        0












        0








        0







        I'll answer my own question for if someone has the same problem in the future. This is what I did:



                IEnumerable<Type> types = typeof(Startup).Assembly.ExportedTypes
        .Where(type => type.IsClass && !type.IsAbstract)
        .Where(type => type.GetInterface("IName") != null)
        .Where(type => type.GetCustomAttribute<NameAttribute>() != null);

        List<string> rules = new List<string>();

        foreach (Type t in types)
        {
        rules.Add(t.GetCustomAttribute<NameAttribute>().Name);
        }


        Basicly what happens is that I look for all the exported types that implement an interface named "IName" and have the "NameAttribute" attribute. This will return a enumerable list with types.



        Next it is possible to retrieve the custom attribute and put the name in an arraylist.






        share|improve this answer













        I'll answer my own question for if someone has the same problem in the future. This is what I did:



                IEnumerable<Type> types = typeof(Startup).Assembly.ExportedTypes
        .Where(type => type.IsClass && !type.IsAbstract)
        .Where(type => type.GetInterface("IName") != null)
        .Where(type => type.GetCustomAttribute<NameAttribute>() != null);

        List<string> rules = new List<string>();

        foreach (Type t in types)
        {
        rules.Add(t.GetCustomAttribute<NameAttribute>().Name);
        }


        Basicly what happens is that I look for all the exported types that implement an interface named "IName" and have the "NameAttribute" attribute. This will return a enumerable list with types.



        Next it is possible to retrieve the custom attribute and put the name in an arraylist.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 9:47









        kjwdammekjwdamme

        486




        486
































            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%2f53315197%2fnet-core-list-properties-of-an-attribute%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python