I have a custom attribute class defined as follows.
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
internal class EncryptedAttribute : System.Attribute
{
private bool _encrypted;
public EncryptedAttribute(bool encrypted)
{
_encrypted = encrypted;
}
public virtual bool Encrypted
{
get
{
return _encrypted;
}
}
}
I applied the above attribute to another class as follows.
public class KeyVaultConfiguration
{
[Encrypted(true)]
public string AuthClientId { get; set; } = "";
public string AuthClientCertThumbprint { get; set; } = "";
}
How do I get the value of Encrypted=True on property AuthClientId?
var config = new KeyVaultConfiguration();
// var authClientIdIsEncrypted = ??
In .NET Framework, this was easy. In .NET CORE, I think this is possible but I don't see any documentation. I believe you need to use System.Reflection but exactly how?
Add using System.Reflection
and then you may use extension methods from CustomAttributeExtensions.cs.
Something like this should work for you:
typeof(<class name>).GetTypeInfo()
.GetProperty(<property name>).GetCustomAttribute<YourAttribute>();
in your case
typeof(KeyVaultConfiguration).GetTypeInfo()
.GetProperty("AuthClientId").GetCustomAttribute<EncryptedAttribute>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With