I would like to automatically generate code for getters and setters for a specific class type which is using specific type of custom attribute. Basically my use case would be this:
[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple = false)]
public abstract class SomeParentAttributeClass : System.Attribute
{
public SomeParentAttributeClass(int i) { }
}
public class SomeCustomAttributeThatInheritsSomeAttributeParentClass : SomeParentAttributeClass
{
public SomeCustomAttributeThatInheritsSomeAttributeParentClass(int i) : base(i) { }
}
public abstract class SomeAttributeUsingBaseClass
{
public SomeAttributeUsingBaseClass()
{
//Finds all custom attributes of type SomeParentClass in child properties and does stuff using info from them
//Basically generates internal fields at runtime that should be accessed by child through properties that have attribute of type SomeParentAttributeClass
}
//Lots of other functionality
}
public class SomeAttributeUsingChild : SomeAttributeUsingBaseClass {
[SomeCustomAttributeThatInheritsSomeAttributeParentClass(1)]
public string SomeProperty {
//TODO auto generate this getter, this is always in the same format using the name of the property to get the underlaying data
get => (string)this["SomeProperty"].SomeFunction();
}
}
I have a custom attribute base class that implements specific common behavior and specific base class that uses attributes set for its properties and generates internal data structures using info from the properties attributes. The only problem I’m currently facing is that I would like to automatically generate the getter and setter for the specific properties that have the custom attribute annotated but I have not yet found a way to do this. I found this Generate code for classes with an attribute but I could not figure out how to use this kind of approach for the getters and setters for class properties.