GetType() temel sınıf çağrıldığında çoğu türetilmiş bir tür geri dönüş mü? | Netgez.com
SORU
25 NİSAN 2011, PAZARTESİ


GetType() temel sınıf çağrıldığında çoğu türetilmiş bir tür geri dönüş mü?

GetType() temel sınıf çağrıldığında çoğu türetilmiş bir tür geri dönüş mü?

Örnek:

public abstract class A
{
    private Type GetInfo()
    {
         return System.Attribute.GetCustomAttributes(this.GetType());
    }
}

public class B : A
{
   //Fields here have some custom attributes added to them
}

Ya da sadece türetilmiş sınıfları aşağıdaki gibi uygulamak zorunda olacak soyut bir yöntem yapmalıyım?

public abstract class A
{
    protected abstract Type GetSubType();

    private Type GetInfo()
    {
         return System.Attribute.GetCustomAttributes(GetSubType());
    }
}

public class B : A
{
   //Fields here have some custom attributes added to them

   protected Type GetSubType()
   {
       return GetType();
   }
}

CEVAP
25 NİSAN 2011, PAZARTESİ


GetType() gerçek, örneği türünü döndürür. Senin durumunda, arama GetType() örneği B döndürür typeof(B) bile değişken söz konusu olduğu bildirilen bir referans için bir A.

GetSubType() yöntemi için bir sebep yok.

Bunu PaylaÅŸ:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ★ByScrapi★ Designs

    ★ByScrapiâ

    27 AÄžUSTOS 2013
  • Defence Videos

    Defence Vide

    13 Mayıs 2013
  • jesiel santos

    jesiel santo

    15 Ocak 2009