Nothing can compare to the Real Power of programming with attributes. Why, just one pair of square brackets and woosh – my object can be serialize to XML. Woosh – my object can persist to a database table. Woosh – there goes my object over the wire in a digitally signed SOAP payload. One day I expect to see a new item template in Visual Studio – the "Add New All Powerful Attributed Class" template: *
Which begs the question – could there ever be a way to separate attributes from the class definition?**
* Put down the flamethrower and step away - I'm kidding.
**This part was a serious question.
Comments
I kid, I kid.
Seriously, though, attributes often seem like a violation of SOC.
Or you can make a request for a new attribute in the next version of the platform:
[assembly: ApplyAttribute(Type=YourType,Attribute=YourAttribute)].
Or go crazy and put the attributes in xml and... go crazy :D
What would you do with this feature? Would it be able to work across assemblies? Could you add attributes to other people's libraries?
AOP would be cool, and I can see something like Spec#.
And I can see lots of wizardry being required to stitch it all together again, to give a re-unified view in VS or Reflector.
<Attributes>
<!-- something notation to include namespaces -->
<Templates>
<Template name="PowerfulTemplate">
<Table/>
<DataObject/>
<DefaultProperty name="Name"/>
</Template>
</Templates>
<!-- class specific goes here -->
<Classes>
<Class class="My.Demo.Foo">
</Class>
</Classes>
</Attributes>
Templates could be used for classes with the same behaviour and Class tags could be used for individual tweaking.
Of course you'd have to plugin at runtime so querying on attributes will read from the xml file instead from the assemblies directly. Not sure if that is possible though.
@kokos, @Laurent: I've thought about partial classes, partial methods, but it feels wrong. I don't like to use partials myself. I feel they should only be present when a code-generation tool is used.
@Lars: It hurts my eyes :) But, that was something along the lines of what I was thinking.
Ditto for exposing a COM+ service over WCF.
See the following:
KnownTypesAttribute: msdn.microsoft.com/en-us/library/ms730167.aspx
Details on COM+ and how to integrate with WCF via the SvcConfigEditor tool: msdn.microsoft.com/en-us/library/ms732009.aspx
The other downside is this will only work for class attributes and won't help for methods/properties/fields/etc ...
[Awesome]
public partial class Foo
{
[UberMethod]
[SecurityAction...]
public string GetStuff();
[ValidateNotEmpty]
public string Yuck
{
get;
}
}
this would all be syntactic sugar over our current CLR.