Możliwym rozwiązaniem jest poinformowanie użytkownika, kim są obiekty, które implementują go za pomocą [ServiceKnownTypeAttribute]
, a kiedy potrzebujesz znać typy, które implementują, otrzymuj przez odbicie. Przykład:
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();
}
I można odzyskać typy, które realizowane z:
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;
}
Jestem pewien, że kod działa dobrze, po prostu nie masz 'AppDomain'. –
@ BlueEyedBehemoth so Jak dołączyć AppDomain? : D –
Do czego jest to potrzebne? Zazwyczaj nie jest to coś, z czym się bałaganisz. –