SORU
23 HAZİRAN 2011, PERŞEMBE


Bu tip 'T' genel tür veya yöntem parametre olarak kullanmak için bir başvuru türü olmalıdır

Jenerik derinliklerine almak ve şimdi yardım almam gereken bir durum var. 'Sınıf aşağıdaki gibi konu başlığında gösterilir. Türetilmiş bir derleme hatası alıyorum Pek çok mesaj buna benzer görüyorum ama ilişki göremiyorum. Birisi bu çözmek için nasıl bana söyleyebilir misiniz?

using System;
using System.Collections.Generic;


namespace Example
{
    public class ViewContext
    {
        ViewContext() { }
    }

    public interface IModel
    {
    }

    public interface IView<T> where T : IModel 
    {
        ViewContext ViewContext { get; set; }
    }

    public class SomeModel : IModel
    {
        public SomeModel() { }
        public int ID { get; set; }
    }

    public class Base<T> where T : IModel
    {

        public Base(IView<T> view)
        {
        }
    }

    public class Derived<SomeModel> : Base<SomeModel> where SomeModel : IModel
    {

        public Derived(IView<SomeModel> view)
            : base(view)
        {
            SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
            Service<SomeModel> s = new Service<SomeModel>();
            s.Work(m);
        }
    }

    public class Service<SomeModel> where SomeModel : IModel
    {
        public Service()
        {
        }

        public void Work(SomeModel m)
        {

        }
    }
}

CEVAP
23 HAZİRAN 2011, PERŞEMBE


Ama ben kopya verememşüphelibu senin gerçek kod bir kısıtlama bir yere T : class - yaymak için derleyici mutlu, örneğin (hard söylemek için emin olmadan bir yeniden oluşturma örnek):

public class Derived<SomeModel> : Base<SomeModel> where SomeModel : class, IModel
                                                                    ^^^^^
                                                                 see this bit

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • InfinityWard

    InfinityWard

    19 EYLÜL 2006
  • MusicDeluxeTV

    MusicDeluxeT

    14 Mayıs 2010
  • Need for Speed

    Need for Spe

    8 ŞUBAT 2006