For the purposes you describe in comments I think using attributes is little bid overkill.
Just create a static method or factory class with method which will do the same things:
public static MyMain CreateInstance(MyEnum attribute)
{
if(attribute == MyEnum.Value1)
{
return new MyClassA();
}
else
{
//...
}
}
Of course if you have a task to create instance based on the attributes then:
private readonly Dictionary<MyEnum, Type> _Data;
public Factory(Assembly assembly)
{
var myAttributeClasses =
assembly.GetTypes()
.Select(t => new
{
DirevedType = t,
Attribute = (MyAttribute)t.GetCustomAttribute(typeof(MyAttribute))
})
.Where(data => data.Attribute != null);
_Data = new Dictionary<MyEnum, Type>();
foreach(var data in myAttributeClasses)
{
_Data.Add(data.Attribute.EnumValue, data.DerivedType);
}
}
public MyMain CreateInstance(MyEnum enumvalue)
{
Type derivedType;
if(_Data.TryGetValue(enumvalue, out derivedType) == false)
return null;
return Activator.CreateInstance(derivedType);
}
MyMainand derived classes or are they third party or code you can't change?