You cannot intercept an object being created without inserting some sort of code into the class itself, or creating a Factory around the class to manage instantiation.
Assuming you have a class like this:
public class MyData
{
[Captialization]
public string Name { get; set; }
}
With an attribute defined like this:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Class)]
public class CaptializationAttribute : Attribute { }
You can detect and manipulate the properties tagged with various attributes like this:
public static class DataUtilities
{
public static void ApplyAttributes<T>(T obj)
{
// Capitalization attribute
var props = typeof (T).GetProperties().Where(p => p.GetCustomAttributes(typeof (CaptializationAttribute), true).Any());
foreach (var prop in props)
{
// This is just an example, if you use this code you
// should check if the property is a string first!
prop.SetValue(obj, prop.GetValue(obj).ToString().ToUpper());
// Or perform some other manipulation here.
}
}
}
Now, to invoke this code automatically, you need to decide when you want it to occur. If it's well after the instantiation, you'll likely have to call it yourself from somewhere. But if it's during instantiation, you can do one of two things:
Using a Factory:
public static class MyDataFactory
{
public static MyData Create()
{
var myData = new MyData();
DataUtilities.ApplyAttributes(myData);
return myData;
}
}
You'll likely want to use an internal constructor to prevent outside instantiation.
// No instantiation from outside the assembly
internal MyData() { }
Using the constructor:
Add the call to the manipulation utility into your constructor:
public MyData()
{
DataUtilities.ApplyAttributes(this);
}
There are other methods of doing this, such as using a Proxy, Dependency Injection, or as @Yuval Itzchakov mentioned, an AOP framework, but the ones I described are probably the easiest to implement.