मैं एजेंडे नियुक्तियों के साथ काम करने के लिए इंटरफेस और सामान्य वर्गों के एक जोड़े को बनाया है:एक सामान्य प्रकार की बाधा का परिणाम कोई अंतर्निहित संदर्भ रूपांतरण त्रुटि क्यों नहीं होता है?
interface IAppointment<T> where T : IAppointmentProperties
{
T Properties { get; set; }
}
interface IAppointmentEntry<T> where T : IAppointment<IAppointmentProperties>
{
DateTime Date { get; set; }
T Appointment { get; set; }
}
interface IAppointmentProperties
{
string Description { get; set; }
}
class Appointment<T> : IAppointment<T> where T : IAppointmentProperties
{
public T Properties { get; set; }
}
class AppointmentEntry<T> : IAppointmentEntry<T> where T : IAppointment<IAppointmentProperties>
{
public DateTime Date { get; set; }
public T Appointment { get; set; }
}
class AppointmentProperties : IAppointmentProperties
{
public string Description { get; set; }
}
मैं प्रकार पैरामीटर पर कुछ बाधाओं का उपयोग करने के लिए सुनिश्चित करें कि वैध प्रकार निर्दिष्ट किया जा सकता कोशिश कर रहा हूँ।
class MyAppointment : Appointment<MyAppointmentProperties>
{
}
// This goes wrong:
class MyAppointmentEntry : AppointmentEntry<MyAppointment>
{
}
class MyAppointmentProperties : AppointmentProperties
{
public string ExtraInformation { get; set; }
}
त्रुटि है:
The type 'Example.MyAppointment' cannot be used as type parameter 'T' in the generic type or method 'Example.AppointmentEntry<T>'. There is no implicit reference conversion from 'Example.MyAppointment' to 'Example.IAppointment<Example.IAppointmentProperties>'.
किसी को क्यों समझा सकते हैं हालांकि, जब एक बाधा को परिभाषित है कि T
IAppointment<IAppointmentProperties>
को लागू करना चाहिए निर्दिष्ट करते समय, संकलक एक त्रुटि जब एक वर्ग है कि है Appointment<AppointmentProperties>
का उपयोग कर देता है यह काम नहीं करता?
यह विषम है। ** लेकिन **: यह जेनेरिकों का एक उग्र अति प्रयोग है। मैं मुश्किल से पढ़ सकता हूं (मुझे लगता है) बहुत, बहुत सरल कोड। –