How does NETCORE get the attribute values of custom properties?












0















1.development environment:




  • C#

  • NetCore-2.1


2.First, I define the following characteristics



public class DynaimcInfo : System.Attribute
{
public DynaimcInfo(int type, string module, string model)
{
this.Type = type;
this.Module = module;
this.Model = model;
}
public int Type { get; }
public string Module { get; }
public string Model { get; }
}


3.I used custom features in a certain class of methods.



[DynaimcInfo(1,"Test","No.1")]
public void Test()
{
//How do I get the attribute values of custom attributes here?
}


4.My goal is to write a log uniformly when calling a method, and the parameters of the feature will determine the module information of the log writing.



[DynaimcInfo(1,"Test1","No.1")]
public void Test1()
{
xx.log(1,"Test","No.1")
}
[DynaimcInfo(2,"Test2","No.2")]
public void Test()
{
xx.log(2,"Test2","No.2")
}


Problem:How do I get the attribute values type, model and module in the custom method test ()?










share|improve this question

























  • You wouldn't. Attributes are statically compiled. Therefore, all information you need about the attribute is present when you write the code. So why create a weird attribute when you can just put that info in the code itself? Attributes are designed to be used by framework code that doesn't know the details when the code is written

    – Erik Funkenbusch
    Nov 14 '18 at 15:49











  • My goal is to get the parameters of writing log in the method. In order to write log, we need to know the module and attributes of the method, which belong to the method information. So I think it would be better to put this information into the features of the method. Do you have any better suggestions?

    – MagicalConch
    Nov 15 '18 at 0:30











  • Again, since you have to put this code in your source code anyways, there is no real advantage to using an attribute here. Attributes are typically used when you have some framework that calls your code. For instance, when using Aspect Oriented Programming, or with MVC (where the framework examines the attributes and makes decisions based on them). You could use an attribute to write to the log (using the parameters) but you still need something that will dispatch that function, and without a framework to do that it's pointless.

    – Erik Funkenbusch
    Nov 15 '18 at 16:09











  • You might look at the PostSharp framework to implement what you want to do. There's also an article here nearsoft.com/blog/…

    – Erik Funkenbusch
    Nov 15 '18 at 16:12













  • The point here, though.. is that .net core doesn't get any attribute properties other than the ones that it, itself supports. Anything custom you would have to write custom reflection code to observe them and potentially act on them. That's what those AOP frameworks do.

    – Erik Funkenbusch
    Nov 15 '18 at 16:18
















0















1.development environment:




  • C#

  • NetCore-2.1


2.First, I define the following characteristics



public class DynaimcInfo : System.Attribute
{
public DynaimcInfo(int type, string module, string model)
{
this.Type = type;
this.Module = module;
this.Model = model;
}
public int Type { get; }
public string Module { get; }
public string Model { get; }
}


3.I used custom features in a certain class of methods.



[DynaimcInfo(1,"Test","No.1")]
public void Test()
{
//How do I get the attribute values of custom attributes here?
}


4.My goal is to write a log uniformly when calling a method, and the parameters of the feature will determine the module information of the log writing.



[DynaimcInfo(1,"Test1","No.1")]
public void Test1()
{
xx.log(1,"Test","No.1")
}
[DynaimcInfo(2,"Test2","No.2")]
public void Test()
{
xx.log(2,"Test2","No.2")
}


Problem:How do I get the attribute values type, model and module in the custom method test ()?










share|improve this question

























  • You wouldn't. Attributes are statically compiled. Therefore, all information you need about the attribute is present when you write the code. So why create a weird attribute when you can just put that info in the code itself? Attributes are designed to be used by framework code that doesn't know the details when the code is written

    – Erik Funkenbusch
    Nov 14 '18 at 15:49











  • My goal is to get the parameters of writing log in the method. In order to write log, we need to know the module and attributes of the method, which belong to the method information. So I think it would be better to put this information into the features of the method. Do you have any better suggestions?

    – MagicalConch
    Nov 15 '18 at 0:30











  • Again, since you have to put this code in your source code anyways, there is no real advantage to using an attribute here. Attributes are typically used when you have some framework that calls your code. For instance, when using Aspect Oriented Programming, or with MVC (where the framework examines the attributes and makes decisions based on them). You could use an attribute to write to the log (using the parameters) but you still need something that will dispatch that function, and without a framework to do that it's pointless.

    – Erik Funkenbusch
    Nov 15 '18 at 16:09











  • You might look at the PostSharp framework to implement what you want to do. There's also an article here nearsoft.com/blog/…

    – Erik Funkenbusch
    Nov 15 '18 at 16:12













  • The point here, though.. is that .net core doesn't get any attribute properties other than the ones that it, itself supports. Anything custom you would have to write custom reflection code to observe them and potentially act on them. That's what those AOP frameworks do.

    – Erik Funkenbusch
    Nov 15 '18 at 16:18














0












0








0








1.development environment:




  • C#

  • NetCore-2.1


2.First, I define the following characteristics



public class DynaimcInfo : System.Attribute
{
public DynaimcInfo(int type, string module, string model)
{
this.Type = type;
this.Module = module;
this.Model = model;
}
public int Type { get; }
public string Module { get; }
public string Model { get; }
}


3.I used custom features in a certain class of methods.



[DynaimcInfo(1,"Test","No.1")]
public void Test()
{
//How do I get the attribute values of custom attributes here?
}


4.My goal is to write a log uniformly when calling a method, and the parameters of the feature will determine the module information of the log writing.



[DynaimcInfo(1,"Test1","No.1")]
public void Test1()
{
xx.log(1,"Test","No.1")
}
[DynaimcInfo(2,"Test2","No.2")]
public void Test()
{
xx.log(2,"Test2","No.2")
}


Problem:How do I get the attribute values type, model and module in the custom method test ()?










share|improve this question
















1.development environment:




  • C#

  • NetCore-2.1


2.First, I define the following characteristics



public class DynaimcInfo : System.Attribute
{
public DynaimcInfo(int type, string module, string model)
{
this.Type = type;
this.Module = module;
this.Model = model;
}
public int Type { get; }
public string Module { get; }
public string Model { get; }
}


3.I used custom features in a certain class of methods.



[DynaimcInfo(1,"Test","No.1")]
public void Test()
{
//How do I get the attribute values of custom attributes here?
}


4.My goal is to write a log uniformly when calling a method, and the parameters of the feature will determine the module information of the log writing.



[DynaimcInfo(1,"Test1","No.1")]
public void Test1()
{
xx.log(1,"Test","No.1")
}
[DynaimcInfo(2,"Test2","No.2")]
public void Test()
{
xx.log(2,"Test2","No.2")
}


Problem:How do I get the attribute values type, model and module in the custom method test ()?







c# asp.net-core attributes






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 0:34







MagicalConch

















asked Nov 14 '18 at 8:28









MagicalConchMagicalConch

364




364













  • You wouldn't. Attributes are statically compiled. Therefore, all information you need about the attribute is present when you write the code. So why create a weird attribute when you can just put that info in the code itself? Attributes are designed to be used by framework code that doesn't know the details when the code is written

    – Erik Funkenbusch
    Nov 14 '18 at 15:49











  • My goal is to get the parameters of writing log in the method. In order to write log, we need to know the module and attributes of the method, which belong to the method information. So I think it would be better to put this information into the features of the method. Do you have any better suggestions?

    – MagicalConch
    Nov 15 '18 at 0:30











  • Again, since you have to put this code in your source code anyways, there is no real advantage to using an attribute here. Attributes are typically used when you have some framework that calls your code. For instance, when using Aspect Oriented Programming, or with MVC (where the framework examines the attributes and makes decisions based on them). You could use an attribute to write to the log (using the parameters) but you still need something that will dispatch that function, and without a framework to do that it's pointless.

    – Erik Funkenbusch
    Nov 15 '18 at 16:09











  • You might look at the PostSharp framework to implement what you want to do. There's also an article here nearsoft.com/blog/…

    – Erik Funkenbusch
    Nov 15 '18 at 16:12













  • The point here, though.. is that .net core doesn't get any attribute properties other than the ones that it, itself supports. Anything custom you would have to write custom reflection code to observe them and potentially act on them. That's what those AOP frameworks do.

    – Erik Funkenbusch
    Nov 15 '18 at 16:18



















  • You wouldn't. Attributes are statically compiled. Therefore, all information you need about the attribute is present when you write the code. So why create a weird attribute when you can just put that info in the code itself? Attributes are designed to be used by framework code that doesn't know the details when the code is written

    – Erik Funkenbusch
    Nov 14 '18 at 15:49











  • My goal is to get the parameters of writing log in the method. In order to write log, we need to know the module and attributes of the method, which belong to the method information. So I think it would be better to put this information into the features of the method. Do you have any better suggestions?

    – MagicalConch
    Nov 15 '18 at 0:30











  • Again, since you have to put this code in your source code anyways, there is no real advantage to using an attribute here. Attributes are typically used when you have some framework that calls your code. For instance, when using Aspect Oriented Programming, or with MVC (where the framework examines the attributes and makes decisions based on them). You could use an attribute to write to the log (using the parameters) but you still need something that will dispatch that function, and without a framework to do that it's pointless.

    – Erik Funkenbusch
    Nov 15 '18 at 16:09











  • You might look at the PostSharp framework to implement what you want to do. There's also an article here nearsoft.com/blog/…

    – Erik Funkenbusch
    Nov 15 '18 at 16:12













  • The point here, though.. is that .net core doesn't get any attribute properties other than the ones that it, itself supports. Anything custom you would have to write custom reflection code to observe them and potentially act on them. That's what those AOP frameworks do.

    – Erik Funkenbusch
    Nov 15 '18 at 16:18

















You wouldn't. Attributes are statically compiled. Therefore, all information you need about the attribute is present when you write the code. So why create a weird attribute when you can just put that info in the code itself? Attributes are designed to be used by framework code that doesn't know the details when the code is written

– Erik Funkenbusch
Nov 14 '18 at 15:49





You wouldn't. Attributes are statically compiled. Therefore, all information you need about the attribute is present when you write the code. So why create a weird attribute when you can just put that info in the code itself? Attributes are designed to be used by framework code that doesn't know the details when the code is written

– Erik Funkenbusch
Nov 14 '18 at 15:49













My goal is to get the parameters of writing log in the method. In order to write log, we need to know the module and attributes of the method, which belong to the method information. So I think it would be better to put this information into the features of the method. Do you have any better suggestions?

– MagicalConch
Nov 15 '18 at 0:30





My goal is to get the parameters of writing log in the method. In order to write log, we need to know the module and attributes of the method, which belong to the method information. So I think it would be better to put this information into the features of the method. Do you have any better suggestions?

– MagicalConch
Nov 15 '18 at 0:30













Again, since you have to put this code in your source code anyways, there is no real advantage to using an attribute here. Attributes are typically used when you have some framework that calls your code. For instance, when using Aspect Oriented Programming, or with MVC (where the framework examines the attributes and makes decisions based on them). You could use an attribute to write to the log (using the parameters) but you still need something that will dispatch that function, and without a framework to do that it's pointless.

– Erik Funkenbusch
Nov 15 '18 at 16:09





Again, since you have to put this code in your source code anyways, there is no real advantage to using an attribute here. Attributes are typically used when you have some framework that calls your code. For instance, when using Aspect Oriented Programming, or with MVC (where the framework examines the attributes and makes decisions based on them). You could use an attribute to write to the log (using the parameters) but you still need something that will dispatch that function, and without a framework to do that it's pointless.

– Erik Funkenbusch
Nov 15 '18 at 16:09













You might look at the PostSharp framework to implement what you want to do. There's also an article here nearsoft.com/blog/…

– Erik Funkenbusch
Nov 15 '18 at 16:12







You might look at the PostSharp framework to implement what you want to do. There's also an article here nearsoft.com/blog/…

– Erik Funkenbusch
Nov 15 '18 at 16:12















The point here, though.. is that .net core doesn't get any attribute properties other than the ones that it, itself supports. Anything custom you would have to write custom reflection code to observe them and potentially act on them. That's what those AOP frameworks do.

– Erik Funkenbusch
Nov 15 '18 at 16:18





The point here, though.. is that .net core doesn't get any attribute properties other than the ones that it, itself supports. Anything custom you would have to write custom reflection code to observe them and potentially act on them. That's what those AOP frameworks do.

– Erik Funkenbusch
Nov 15 '18 at 16:18












0






active

oldest

votes











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%2f53295858%2fhow-does-netcore-get-the-attribute-values-of-custom-properties%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53295858%2fhow-does-netcore-get-the-attribute-values-of-custom-properties%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

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values