2012-08-07 8 views
5

निम्न त्रुटि हो रही है:सी # जेनेरिक त्रुटि: विधि के प्रकार पैरामीटर 'टी' के लिए बाधाएं ...?

Error 1 The constraints for type parameter ' T ' of method
' genericstuff.Models.MyClass.GetCount<T>(string) ' must match the constraints for type
parameter ' T ' of interface method ' genericstuff.IMyClass.GetCount<T>(string) '. Consider
using an explicit interface implementation instead.

कक्षा:

public class MyClass : IMyClass 
{ 
    public int GetCount<T>(string filter) 
    where T : class 
     { 
     NorthwindEntities db = new NorthwindEntities(); 
     return db.CreateObjectSet<T>().Where(filter).Count(); 
     } 
} 

इंटरफ़ेस:

public interface IMyClass 
{ 
    int GetCount<T>(string filter); 
} 

उत्तर

16

आप अपने कार्यान्वयन में कक्षा में अपने टी सामान्य पैरामीटर सीमित कर रहे हैं। आपके इंटरफ़ेस पर यह बाधा नहीं है।

आप अपने वर्ग से हटाने या अपने इंटरफेस के लिए इसे जोड़ने कोड संकलन जाने के लिए की जरूरत है:

आप विधि CreateObjectSet<T>(), जो requires the class constraint, आप अपने इंटरफेस में जोड़ने के लिए की जरूरत है बुला रहे हैं के बाद से।

public interface IMyClass 
{ 
    int GetCount<T>(string filter) where T : class; 
} 
+0

हे डिकी ने आदमी को – user603007

+0

एर लोपेन हायर सर्वश्रेष्ठ वाट नेडरलैंडर्स रैंड इंडरडाड! :) –

+0

OZ wat minder में hier :) वैसे भी धन्यवाद – user603007

3

आपको या तो इंटरफ़ेस विधि में बाधा लागू करने या कार्यान्वयन से इसे हटाने की आवश्यकता है।

आप कार्यान्वयन पर बाधा को बदलकर इंटरफ़ेस अनुबंध बदल रहे हैं - इसकी अनुमति नहीं है।

public interface IMyClass 
{ 
    int GetCount<T>(string filter) where T : class; 
} 
1

आपको भी अपना इंटरफ़ेस बाध्य करने की आवश्यकता है।

public interface IMyClass 
{ 
    int GetCount<T>(string filter) where T : class; 
} 
संबंधित मुद्दे