अच्छा, मेरा मानना है कि इसे सामान्य प्रकार पैरामीटर के मामले में अनुमति दी जानी चाहिए। यह शायद संविदात्मक सिंगलटन वर्ग को सरल बना दिया।
public interface IEntity {
// some constrains...
DataRow ObjToRow(object obj);
object RowToObj(DataRow dr);
}
//T would be any class inherites from IEntity with default contructor signature.
public interface IMyContract {
T read<T>() where T : IEntity;
void write<T>(T object) where T : IEntity;
}
//everything in the class is static
public static class SqlProvider : IMyContract {
public static T read<T>() where T: IEntity {
DataRow dr = [reading from database]
return T.RowToObj(dr);
}
//compile error here....
public static void write<T>(T obj) where T : IEntity {
DataRow dr = T.ObjToRow(obj);
[ ... commit data row dr to database ... ]
}
}
public static class MyAppleEntity : IEntity {
[... implement IEntity contract normally ... ]
}
public static class MyOrangeEntity : IEntity {
[... implement IEntity contract normally ... ]
}
public class MyTest {
void reading() {
MyAppleEntity apple = SqlProvider.Read<MyAppleEntity>();
MyOrangeEntity orange = SqlProvider.Read<MyOrangeEntity>();
SqlProvider.write<MyAppleEntity>(apple);
SqlProvider.write<MyOrangeEntity>(orange);
}
}
केवल समय एक प्रकार संदर्भ परोक्ष) SqlProvider.read (में है और लिखने() और टी आह्वान के बिंदु पर अच्छी तरह से पहचान है: यहाँ एक उदाहरण है। इंटरफेस के स्थिर कार्यान्वयन के बिना मुझे इस तरह लिखने के लिए मजबूर किया गया है।
public class MyAppleEntity : IEntity {
[... implement IEntity contract normally ... ]
}
.....
public T read<T>() where T: IEntity, new() {
DataRow dr = [reading from database]
return new T().RowToObj(dr);
}
बहुत कम अलग लेकिन सुरुचिपूर्ण नहीं है।
यह समझ में नहीं आता है। XXX के व्युत्पन्न वर्ग में आप इंटरफ़ेस को कैसे कार्यान्वित करेंगे? हालांकि, कार्यान्वयन से स्थिर सदस्य को कॉल करने का कोई कारण नहीं है। – leppie
http://stackoverflow.com/questions/259026/why-doesnt-c-allow-static-methods-to-implement-an-interface – bernhof
@leppie, आप ** ** ** "टाइप विधि" हो सकते हैं; जहां कोई अंतर्निहित नहीं था, लेकिन इस प्रकार से रनटाइम पर विधि को हल किया गया। दुर्लभ मौकों का उपयोग करने के लिए उन्हें या तो उदाहरण के तरीकों या प्रतिबिंब के लिए कैटर किया जा सकता है, इसलिए दबाव डालने की आवश्यकता नहीं है आईएमओ। –