2016-07-18 4 views
10

में एक अंतरफलक प्रतिबिंब का उपयोग लागू हो रही है मैं कैसे नेट कोर में सभी प्रकार है कि कुछ विशिष्ट इंटरफ़ेस को लागू कर सकते हैं? मैंने देखा है कि .NET 4.6 में उपयोग करने योग्य विधियां अब उपलब्ध नहीं हैं।, सभी प्रकार के जो .NET कोर

उदाहरण के लिए, इस कोड काम नहीं करता।

var type = typeof(IMyInterface); 
var types = AppDomain.CurrentDomain.GetAssemblies() 
    .SelectMany(s => s.GetTypes()) 
    .Where(p => type.IsAssignableFrom(p)); 

यह The name 'AppDomain' does not exist in the current context त्रुटि फेंकता है।

+2

मुझे यकीन है कि कोड ठीक काम करता है हूँ, तुम सिर्फ एक 'AppDomain' जरूरत नहीं है। –

+0

@ ब्लूएड बेहेमोथ तो ऐपडोमेन कैसे शामिल करें? : डी –

+0

आपको इसके लिए क्या चाहिए? आमतौर पर ऐसा कुछ नहीं होता जिसके साथ आप गड़बड़ करते हैं। –

उत्तर

9

आप इस तरह से कर सकते हैं:

System.Reflection.Assembly ass = System.Reflection.Assembly.GetEntryAssembly(); 

foreach (System.Reflection.TypeInfo ti in ass.DefinedTypes) 
{ 
    if (ti.ImplementedInterfaces.Contains(typeof(yourInterface))) 
    { 
     ass.CreateInstance(ti.FullName) as yourInterface; 
    } 
} 

आप सभी विधानसभाओं में प्रकार चाहते हैं, बस सभी संदर्भों को प्राप्त करने के लिए निम्नलिखित का उपयोग करें और फिर से ऊपर करना :)

ass.GetReferencedAssemblies() 
+0

यह केवल एक ही असेंबली के भीतर प्रकार प्राप्त करता है, सभी लोड असेंबली नहीं। –

+1

LINQ var प्रकार = विधानसभा.GetEntryAssembly()। परिभाषित प्रकार। (Ti => ti.ImplementedInterfaces.Contains (टाइपऑफ (ILogger)) का चयन करें। (M => m.FullName) का चयन करें। –

2

जहां तक ​​मेरा बता सकते हैं, वहाँ नेट कोर 1.0 में सभी लोड विधानसभाओं प्राप्त करने के लिए कोई रास्ता नहीं है। It seems a way to do this is planned for 1.1.

0

यदि आप सभी असेंबली में प्रकार चाहते हैं, तो बस सभी संदर्भ प्राप्त करने के लिए निम्न का उपयोग करें और उपरोक्त कार्य करें :)

ass.GetReferencedAssemblies() 
0

सम्भावित समाधान इंटरफेस करने जो वस्तुओं है कि यह [ServiceKnownTypeAttribute] साथ लागू करने और आप प्रकार है कि लागू परछाई से मिल पता है की जरूरत है जब कर रहे हैं बताते हैं। उदाहरण:

public class TypeWithImplementOne : IMyInterface 
{ 
    public string Hi() 
    { 
    return "hi"; 
    } 

} 
public class TypeWithImplementTwo : IMyInterface 
{ 
    public string Hi() 
    { 
    return "hi"; 
    } 
} 
public interface IMyInterface{ 
{ 
    [ServiceKnownType(typeof(TypeWithImplementOne))] 
    [ServiceKnownType(typeof(TypeWithImplementTwo))] 

    string Hi(); 
} 

और तुम प्रकार है कि के साथ लागू की वसूली कर सकते हैं:

private IEnumerable<string> GetKnownTypes() 
    { 
     List<string> result = new List<string>(); 

     Type interfaceType = typeof(IMyInterface); 
     IEnumerable<CustomAttributeData> attributes = interfaceType.CustomAttributes 
      .Where(t => t.AttributeType == typeof(ServiceKnownTypeAttribute)); 

     foreach (CustomAttributeData attribute in attributes) 
     { 
      IEnumerable<CustomAttributeTypedArgument> knownTypes = attribute.ConstructorArguments; 
      foreach (CustomAttributeTypedArgument knownType in knownTypes) 
      { 
       result.Add(knownType.Value.ToString()); 
      } 
     } 

     result.Sort(); 
     return result; 
    } 
2

पूर्ण कोड सभी प्राप्त करने के लिए।

public static IEnumerable<T> GetAll() 
{ 
    var assembly = Assembly.GetEntryAssembly(); 
    var assemblies = assembly.GetReferencedAssemblies(); 

    foreach (var assemblyName in assemblies) 
    { 
     assembly = Assembly.Load(assemblyName); 

     foreach (var ti in assembly.DefinedTypes) 
     { 
      if (ti.ImplementedInterfaces.Contains(typeof(T))) 
      { 
       yield return (T)assembly.CreateInstance(ti.FullName); 
      } 
     } 
    }    
} 
1

नेट कोर 2.0 में, आप विधानसभाओं कि संकलन समय पर जाने जाते थे में मिलान करने वाले सभी प्रकार के (इस गतिशील रूप से लोड विधानसभाओं के लिए काम नहीं करता है) इस तरह पा सकते हैं:

private static IEnumerable<Type> GetAllTypesOf<T>() 
{ 
    var platform = Environment.OSVersion.Platform.ToString(); 
    var runtimeAssemblyNames = DependencyContext.Default.GetRuntimeAssemblyNames(platform); 

    return runtimeAssemblyNames 
     .Select(Assembly.Load) 
     .SelectMany(a => a.ExportedTypes) 
     .Where(t => typeof(T).IsAssignableFrom(t)); 
} 

इस पर निर्भर करता है Microsoft.Extensions.DependencyModel पैकेज।