jeudi 9 mai 2019

Get Type From null object

First:

public class ClassePrincipal
{
    [Key] public long IdPrinc { get; set; }
    public string NomePrinc { get; set; }
    public string Princ { get; set; }


    public override string ToString()
    {
        return "ID: " + IdPrinc + " NomePrinc: " + NomePrinc + " Princ: " + Princ;
    }
}

Second:

public class Classe_Secundaria
{
    [Key] public long IdSec { get; set; }
    public string NomeSec { get; set; }
    public string Sec { get; set; }


    [Primary] public ClassePrincipal ClassePrincipal { get; set; }

    public override string ToString()
    {
        return "ID Sec: " + IdSec + " NomeSec: " + NomeSec + " Sec: " + Sec + (ClassePrincipal != null ? " ClassePrincipal: " + ClassePrincipal.ToString() : "");

    }
}

I use this:

Classe_Secundaria x = null;

I need get Type from x, but x is Null, how can I get Type from x using reflection? without reflection I used:

Type type = TypedReference.GetTargetType(__makeref(x));

When I use this code in reflection he got only Object Type, and not Classe_Secundaria type





Aucun commentaire:

Enregistrer un commentaire