tags:

views:

74

answers:

3

Alright, so after a few hours of me playing around 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 that if somebody were 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 somebody was trying to access PrivateProperty, they could just go:

ContainingClass.PublicProperty = //ect

"PublicProperty". and that is because of the attribute, and it would use those get/set accessors.

What I'd like to know:

  1. Is this even possible?
  2. Is there something that already does this?
  3. If its possible, (even if there is something else) How do i do this?
A: 

Microsoft made the WebMethodAttribute in a way reminiscent of what you're trying to describe making it represent more permission than C# public, effectively making a method available outside the application domain to the entire Internet (a very global scope). You might read it to get real implementation insight and ideas.

But you're hitting it very simply. You'll have to program some infrastructure to make it work. It's not automatic and you don't have access to Microsoft's source code for all the details.

John K
WebMethodAttribute is kinda different. It instructs the Web services routing infrastructure to surface a SOAP operation for the method. It doesn't actually create additional compile-time members (in sniperX's case, a property) on the class.
itowlson
+1  A: 

No, this is not possible using attributes. Properties are part of the class metadata emitted by the C# compiler, and the C# compiler does not consider custom attributes.

You may be able to do this by using a post-processor such as PostSharp, which can rewrite your assembly after the fact, and can be instructed to consider custom attributes. However, you still wouldn't be able to include a delegate in the attribute: the set of types that can be stored in attribute state is extremely limited.

itowlson
I think `ContainingClass.PublicProperty = //ect` would still not compile, even with an AOP post-compiler
Chris S
Having said that, +1 as postsharp is probably what he's after
Chris S
Depends on whether PostSharp is able to emit additional properties, rather than just rewriting existing methods. I'm not sufficiently familiar with PostSharp to know whether this is part of its repertoire, but there's no technical reason a post-compiler couldn't do this.
itowlson
On second thoughts, you're right. It *would* work when accessed from another project that referenced the post-processed assembly, but not *within* the project where he declared ContainingClass. Big facepalms to me.
itowlson
+3  A: 

Basically no to all 3, as C# is a strongly typed language. Even with duck typing what you're trying to achieve doesn't fit the language.

The attributes you've written allow you to interrogate the properties that have those attributes in the class, but you still need to use Reflection to discover which properties of the attribute class are set. The syntax you want to use is checked at compile-time.

Chris S