Hej
I have the following setup:
Assembly 1
public abstract class XX<T> : XX where T: YY { }
public abstract class XX {}
Assembly 2
public class ZZ : YY {}
public class ZZFriend : XX<ZZ> {}
I use this feature in reflection when in YY:
public class YY {
public Type FindFriend {
return GetType().Assembly.GetTypes().FirstOrDefault(
t => t.BaseType != null &&
t.BaseType.IsGenericType &&
typeof(XX).IsAssignableFrom(t) &&
t.BaseType.GetGenericArguments().FirstOrDefault() == GetType());
}
}
I would like do disallow inheritance of the non generic class XX like:
public class ZZFriend: XX {}
Alternatively, I need a method like (that can be used in the reflection in YY.FindFrind()):
public Type F(Type t) {
return GetTypeThatIsGeneric(XX, Type genericTypeParameter);
}
That can be used in YY as:
Typeof(XX<ZZ) == F(typeof(GetType())
Hope that makes sense...
Thanks in advance
Søren Rokkedal
You can create an internal constructor in XX
:
public abstract class XX
{
internal XX()
{
}
}
(Or if you already have explicitly-declared constructors, make them all private or internal.)
That will prevent any other assemblies from declaring derived classes, because there's be no constructor to chain to.
See more on this question at Stackoverflow