I have an empty IContext
inteface:
public interface IContext {
}
I also have two derived interfaces IBasicContext
and ITypedContext
:
public interface IBasicContext : IContext {
string Id { get; }
}
public interface ITypedContext<T> : IContext {
T Value { get; }
}
I have another project with some code that processes these contexts:
internal static ProcessedContext Process(this IContext context) {
if (context is IBasicContext basicContext) {
return basicContext.Process();
} else if (context.GetType().IsAssignableFrom(typeof(ITypedContext<>))){
// What do I do here??
}
}
internal static ProcessedContext Process(this IBasicContext context) {
// Do stuff here to create processed context
}
internal static ProcessedContext Process<T>(this ITypedContext<T> context) {
// Do stuff here to create processed context
}
Note 1: I have already checked multiple posts. Most of them ask about casting to a base generic class, which is NOT what I am trying to do here.
Note 2: Context classes sit in their own project. They are merely data structures and ProcessedContext
creation code does not belong in context project.
Note 3: T
can be one of multiple types that I only create at runtime. Having multiple cases for each type is just daunting and ugly. The processing of ITypedContext
does not really care about T
. It calls another generic method.