Personally, I think C# and visual studio is the best toolset I've ever used in like a zillion years in IT.
Here's one small reason why. In C#, you can add attribution to object properties and methods.
Create a workflow, examine the properties, and you may see something like this:
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
or this for a WCF service:
[ServiceContract]
public interface SomeService
{
[OperationContract]
This attribution is basically metadata about the properties and meathods. Microsoft uses these for a whole list of things. One of the most obvious uses is to be able to provide meaningful information to visual editors and other 3rd party tools. An add on product, for example, that does unit testing -- nUnit, mbUnit and even Microsoft's own testing set -- uses this metadata to identify the entry points for tests and the test methods with attributes like
[TestFixture] and [TestMethod]
This is great. And it gives .Net a real professionalism over other great tools powered by Java or C++ or Php or other things.
What's even better, is that the attribution is extentable, so that you can create your own attributes. More, doing that is easy.
Simply create a class that subclasses the "Attribute" class in .net's System namespace. For example, suppose I wanted a custom attribute to indicate which properties were to be persisted to a database. This is similar to the "Serializable" attribute, but suppose I wanted to roll my own.
All I would do is this:
[AttributeUsage(AttributeTargets.All)]
public class DBSerializable: Attribute
As long as the class is in a namespace to which the target code has a reference (ie, "using") you can add
[DBSerializable]
to any property of the target class. Visual studio's intellesense will pick it up and "tada" instant custom attribution.
Ok, so I've got it... now what do I *do* with it?
Well, in this case, all I want to do is identify which properties have this attribute so that I can serialize them or not.
Since .Net's reflection pulls the meta-data about the properites, this means it pulls the attributes too -- even the custom ones!
So this becomes possible:
public static PropertyInfo[] myCustomAttributeDemo(object o) // input type of "object"
{
Type t = o.GetType(); // get the type of object
PropertyInfo[] pi = t.GetProperties(); // get the properties for the object
foreach (PropertyInfo p in pi) // for each property in the object
{
object[] q = p.GetCustomAttributes(true); // get the attributes
for (int ii = 0; ii < class="Apple-tab-span" style="white-space:pre"> // for each attribute
{
if (q[ii].GetType() == typeof(Util.DBSerializable)) // check the type
{
// do some custom logic for serializable properties
}
}
}
This can be really handy for writing really generic, reusable code. For example this was part of a generic object serializer that will generate insert statements for specific databases based on any object passed in. Doing that really cuts down on the amount of (basically) duplicate code. And it just has the small assumption that the persistable properties use the custom attribute.
Very cool.
No comments:
Post a Comment