Q:

Statically derived type in C#?

I'm looking for a C# alternative to C++ typedefs or traits classes in a specific case. I know that there are no direct equivalents, but perhaps there are some alternative solutions for this specific problem?
Here is what I'm trying to do. I'm writing a framework where there are three related types. A view, a backing storage and a factory. There will be multiple implementations of all three interfaces. There is a 1-1 relation between the view and the factory and there is a 1-N relation between the view and the storage. A concrete impl. of the framework looks something like this:
Storage : IStorage<int> ...

View : IView<Storage> ... // And IView<T> : IViewNonGeneric further up..

Factory : IFactory<Storage> {
  // This needs to take a concrete storage type as arg
  IViewNonGeneric CreateView(Storage s) ... 

  Storage CreateStorage() ...  
}

The View class is the most important class for the users of the framework; the others are kind of implementation details. Because of this it would seem more natural to define the Factory in terms of the View class (and not in terms of the Storage). In C++ this would be straight forward, just add a typedef to the view and use it in the factory, like so: 
class IView<typename T> { typedef T TStorage; ...

class IFactory<typename T> { 
  IViewNonGeneric CreateView(typename T::TStorage s) ... 

In C# we obviously don't have typedefs or traits classes. Is there any other way to accomplish the desired effect? That is, can one use View as the generic parameter to the Factory and derive the concrete Source type from the View?

A:

Generics in C# are definitely not as powerful as templates in C++.  However, C# does have something very powerful that C++ doesn't have: Reflection.
It should be very easy to define a method (either static or instance) on the view class that would return the concrete type of storage class.  Then you can use Type.GetConstructor to dynamically find a constructor of the storage class and invoke it using ConstructorInfo.Invoke method.
Additionally, you can explore the use of custom attributes that you could assign to your view class. How about something like this:
[StorageType( typeof( MyStorage1 ) ]
class MyView1 { ... }

Then use reflection on typeof( MyView1 ) to see if it has StorageTypeAttribute associated with it.

