mercredi 6 mai 2020

How to get a property using reflection?

I need to remove any reference to System.Data.SqlClient from my project. The only instance I use it is to get a list of errors, for example:

if (ex is SqlException)
{
    var spx = ex as SqlException;
    if (spx.Errors != null)
    {
        for (int i = 0; i < spx.Errors.Count; i++)
        { 
            var sqlError = spx.Errors[i];
        }
    }
}

Is the only alternative to use Reflection and call GetProperty as in:

if (ex.GetType().Name == "SqlException")
{
    var errors = ex.GetType().GetProperty("Errors");
    if (errors != null)
    {}
}




Aucun commentaire:

Enregistrer un commentaire