Cecil - Ottenere l'definita attributi proprietà
-
12-09-2019 - |
Domanda
Sto usando Cecil per cercare di leggere le mie proprietà attributi:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class TraceMethodAttribute : Attribute {
public TraceMethodAttribute() {
MethodStart = true;
MethodReturn = true;
MethodMessages = true;
}
public bool MethodStart { get; set; }
public bool MethodReturn { get; set; }
public bool MethodMessages { get; set; }
}
[TraceMethod(MethodMessages = false)]
static void Main(string[] args) {
}
...
if (attribute.Constructor.DeclaringType.FullName == typeof(TraceMethodAttribute).FullName) {
if ((bool)attribute.Fields["MethodMessages"] == true) {
EditMethodStart(assembly, method);
}
Questa è, mi piacerebbe questo ultimo blocco di codice per controllare ogni volta che l'attributo applicato alla principale, per esempio, ha MethodMessages impostate su true o false. Da quello che ho visto, sembra che sia attributes.Fields.Count e attributes.Properties.Count è impostato su 0. Perché è?
Grazie
Soluzione
dovrebbe funzionare bene attraverso l'accesso ai Properties da indicizzatore.
if (attribute.Constructor.DeclaringType.FullName == typeof(TraceMethodAttribute).FullName) {
if ((bool)attribute.Properties["MethodMessages"] == true) {
EditMethodStart(assembly, method);
}
Basta compilato e controllato.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow