I want to be able to derive from a class internally, but disallow class in other assemblies to derive from the class

Posted by Rokke on Stack Overflow See other posts from Stack Overflow or by Rokke
Published on 2014-06-05T09:23:01Z Indexed on 2014/06/05 9:24 UTC
Read the original article Hit count: 169

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection