Attributes creating fields in C #

Ok, so after several hours of work, when I play to no avail, I built a model:

[AttributeUsage(AttributeTargets.All)]
public class PublicAttribute : System.Attribute
{
    public enum Access { Public, Private }
    public PublicAttribute(string Name, Access acs)
    {
    }
    public PublicAttribute(string Name, Access acs, Action get, Action set)
    {
    }
}

So, if someone had to do something like this:

[Public("PublicProperty", PublicAttribute.Access.Public)]
private string PrivateProperty = "hello";

or

[Public("PublicProperty", PublicAttribute.Access.Public, ()=>{return PrivateProperty;}, ()=>{PrivateProperty = value})]
private string PrivateProperty = "hello";

and then if someone tries to access PrivateProperty, they can just go:

ContainingClass.PublicProperty = // ect

"public property". and this is due to the attribute, and it will be used by those who receive / install accessors.

What I would like to know:

  • Is it possible?
  • Is there something that already does this?
  • If possible, (even if there is something else) How to do this?
+3
source share
3 answers

3, # - . , , .

, , , , Reflection, , . , , .

+4

, . , #, # .

, , , PostSharp, . , : , , .

+1

Microsoft WebMethodAttribute , , , , # public, ( ). , .

. , . Microsoft .

0

Source: https://habr.com/ru/post/1735684/


All Articles