की बाधा का उल्लंघन करता है मैं इस पर थोड़ी देर के लिए अपने बालों को खींच रहा हूं, अनिवार्य रूप से मैं एक सामान्य भंडार कारखाना को लागू करने की कोशिश कर रहा हूं, जिसे निम्नानुसार कहा जाता है:जेनरिक और प्रतिबिंब - जेनेरिक आर्ग्यूमेंट्स [0]
public class RepositoryFactory<T> : IRepositoryFactory<T>
{
public T GetRepository(Guid listGuid,
IEnumerable<FieldToEntityPropertyMapper> fieldMappings)
{
Assembly callingAssembly = Assembly.GetExecutingAssembly();
Type[] typesInThisAssembly = callingAssembly.GetTypes();
Type genericBase = typeof (T).GetGenericTypeDefinition();
Type tempType = (
from type in typesInThisAssembly
from intface in type.GetInterfaces()
where intface.IsGenericType
where intface.GetGenericTypeDefinition() == genericBase
where type.GetConstructor(Type.EmptyTypes) != null
select type)
.FirstOrDefault();
if (tempType != null)
{
Type newType = tempType.MakeGenericType(typeof(T));
ConstructorInfo[] c = newType.GetConstructors();
return (T)c[0].Invoke(new object[] { listGuid, fieldMappings });
}
}
}
जब मैं GetRespository फोन करने की कोशिश में कार्य निम्न पंक्ति में विफल रहता है
Type newType = tempType.MakeGenericType(typeof(T));
:
var resposFactory = new RepositoryFactory<IRepository<Document>>();
भंडार कारखाने ऐसा दिखाई देता है
त्रुटि मैं मिलता है:
ArgumentException - GenericArguments [0], 'Framework.Repositories.IRepository`1 [Apps.Documents.Entities.PerpetualDocument]', 'Framework.Repositories.DocumentLibraryRepository`1 पर [टी] 'प्रकार' टी 'की बाधा का उल्लंघन करता है।
क्या गलत हो रहा है पर कोई विचार?
संपादित करें:
भंडार के कार्यान्वयन निम्नानुसार है:
public class DocumentLibraryRepository<T> : IRepository<T>
where T : class, new()
{
public DocumentLibraryRepository(Guid listGuid, IEnumerable<IFieldToEntityPropertyMapper> fieldMappings)
{
...
}
...
}
और तरह IRepository दिखता है:
public interface IRepository<T> where T : class
{
void Add(T entity);
void Remove(T entity);
void Update(T entity);
T FindById(int entityId);
IEnumerable<T> Find(string camlQuery);
IEnumerable<T> All();
}
क्या आप वहां एक रिटर्न स्टेटमेंट खो रहे हैं? क्या आपने उस विधि की पूरी प्रतिलिपि बनाई है? –
इसके अलावा, आप पैरामीटर रहित कन्स्ट्रक्टर की उपस्थिति की जांच क्यों कर रहे हैं जब आप स्पष्ट रूप से पैरामीटर के साथ एक कन्स्ट्रक्टर का आह्वान करना चाहते हैं? यदि आपके पास पैरामीटर रहित कन्स्ट्रक्टर है, तो यह संभवतः 'GetConstructors' द्वारा लौटाया गया 0 वां कन्स्ट्रक्टर होने वाला है, जिस स्थिति में * पैरामीटर के साथ * कॉलिंग विफल हो जाएगी। –
हां क्षमा करें 'वापसी डिफ़ॉल्ट (टी)' अंत में होना चाहिए। – Bevan