A possible solution is tell to interface who are the objects that implement it with [ServiceKnownTypeAttribute] and when you need know the types that implement get by reflexion. Example:
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();
}
And you can recover the types that implemented with:
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;
}
AppDomain.