我有一种情况,我需要获取应用于类的属性(装饰器)的属性值。那个被装饰的类是从一个抽象类继承的。正是这个抽象类需要获取属性信息,但它需要在静态函数内部进行。
我无法发布确切的场景,但这是一个没有属性的可怕示例,但请按原样使用:
public class VehicleShapeAttribute : Attribute
{
public string Shape { get; }
public VehicleShapeAttribute(string shape)
{
Shape = shape;
}
}
public abstract class Vehicle
{
public string Brand { get; set; }
public string Model { get; set; }
public string Colour { get; set; }
public static string GetVehicleShape()
{
//return value from the attribute, from this static function. CANT DO THIS HERE
return AnyInheritingClass.VehicleShapeAttribute.Shape;
}
}
[VehicleShape("sedan")]
public class VauxhaulAstraSedan : Vehicle
{
//calling GetVehicleShape() on this class should automatically return "sedan"
}
这可能吗?
这是一个不好的例子,但我无法发布实际代码