estou com o seguinte problema, Tenho minha classe ENUM:
public enum DataBaseType
{
DATA_BASE_NAME, SERVER_IP_NAME, PORT, USER_NAME, PASSWORD
};
Tenho o meu Attributo, no qual possui esse ENUM:
public class Connect : System.Attribute
{
public DataBaseType ValueType { get; set; }
}
Tenho minha classe de Conexao, sendo:
class ConnectionModel
{
[Connect(ValueType = DataBaseType.SERVER_IP_NAME)]
string ServerName { get; set; } = "localhost";
[Connect(ValueType = DataBaseType.PORT)]
string Port { get; set; } = "5432";
[Connect(ValueType = DataBaseType.USER_NAME)]
string UserName { get; set; } = "usuario";
[Connect(ValueType = DataBaseType.PASSWORD)]
string Password { get; set; } = "senha";
[Connect(ValueType = DataBaseType.DATA_BASE_NAME)]
string DatabaseName { get; set; } = "banco";
}
Tenho também a leitura dessa classe:
Type type = obj.GetType();
foreach (FieldInfo field in type.GetRuntimeFields())
{
Console.WriteLine(field.Name + ": " + field.GetValue(obj));
}
Até agora está tudo funcionando, porem agora preciso saber qual ValueType foi determinado para cada variavel, então faço o seguinte:
Type type = obj.GetType();
foreach (FieldInfo field in type.GetRuntimeFields())
{
Console.WriteLine(field.Name + ": " + field.GetValue(obj));
Connect attribute = field.GetCustomAttribute<Connect>();
if (attribute != null)
{
MessageBox.Show("Is Not Null: " + attribute.ValueType);
}
}
o Problema é: Toda hora o attribute está dando null, porque cargas dagua isso? onde estou errando?