C# 中的静态派生类型?
我正在寻找特定情况下 C++ typedef 或特征类的 C# 替代方案。我知道没有直接的等价物,但对于这个特定问题也许有一些替代解决方案?
这就是我正在尝试做的事情。我正在编写一个框架,其中包含三种相关类型。视图、后备存储和工厂。所有三个接口都会有多种实现。视图和工厂之间存在 1-1 关系,视图和存储之间存在 1-N 关系。具体的实现。框架的结构看起来像这样:
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() ...
}
View 类对于框架的用户来说是最重要的类;其他的是实施细节。因此,根据视图类(而不是存储)来定义工厂似乎更自然。在 C++ 中,这很简单,只需将 typedef 添加到视图中并在工厂中使用它,如下所示:
class IView<typename T> { typedef T TStorage; ...
class IFactory<typename T> {
IViewNonGeneric CreateView(typename T::TStorage s) ...
在 C# 中,我们显然没有 typedef 或特征类。还有其他方法可以达到想要的效果吗?也就是说,是否可以使用 View 作为 Factory 的通用参数并从 View 派生具体的 Source 类型?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C# 中的泛型绝对不如 C++ 中的模板那么强大。然而,C# 确实有一些 C++ 所没有的非常强大的东西:反射。
在视图类上定义一个返回具体类型的存储类的方法(静态或实例)应该很容易。然后,您可以使用 Type.GetConstructor 动态查找存储类的构造函数,并使用 ConstructorInfo.Invoke 方法调用它。
此外,您还可以探索如何使用可分配给视图类的自定义属性。像这样的事情怎么样:
然后使用 typeof( MyView1 ) 上的反射来查看它是否具有与之关联的 StorageTypeAttribute 。
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:
Then use reflection on typeof( MyView1 ) to see if it has StorageTypeAttribute associated with it.
我想这就是你想要的:
I think is what you want: