将参数typeof(Type)限制为仅包含某些类型 [英] Restrict parameter typeof(Type) to only contain certain Types

查看:100
本文介绍了将参数typeof(Type)限制为仅包含某些类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

我正在玩一个包含在经理类中的启动画面。

这个经理类有一个工厂方法,一切正常。

Good morning all,
I am currently playing around with a splash screen wrapped in a manager class.
This manager class has a factory method and everything works fine.

public static SplashScreenManager Factory(SplashScreenManagerOptions splashScreenManagerOptions)





现在我觉得传入参数会让用户选择什么样的屏幕会很不错他/她希望'使用。



Now I am thinking that it would be nice to pass in a parameter in order to let the user choose what kind of screen he/she want''s to use.

public static SplashScreenManager Factory(SplashScreenBase splashScreenBase, SplashScreenManagerOptions splashScreenManagerOptions)





这里的问题是我必须获取的类型splashScreenBase然后使用反射创建它的新实例,因为splashScreenForm将在其中运行自己的线程。所以用户必须传入一个实例才能创建另一个实例...即使工作正常我也感觉不好因为我不知道什么样的初始化和东西必须发生两次。



所以我想到了使用约束。这就是我到目前为止所提出的:



Problem here is that I have to obtain the Type of the splashScreenBase and then create a new instance of it using reflection, due to the fact that the splashScreenForm will be run in it''s own thread. So the user has to pass in an instance only to have another instance created... even if that works fine I do not feel good because I do not know what kind of initializing and stuff will have to happen twice.

So I thought about using constraints. Here''s what I''ve come up with so far:

public static SplashScreenManager Factory<T>(T splashScreenFormType, SplashScreenManagerOptions splashScreenManagerOptions) where T : SplashScreenBase



好​​吧,这并不是我想的那样......它只限制了SplashScreenBase类型的参数,真的不是一个惊喜...





我真正想做的是以下内容。

参数应该是Type类型,应该可以从SplashScreenBase中分配...

所以基本上它应该是一个限制参数typeof(Type)只包含某些类型。

这有意义吗?我希望如此;)





更新:

这里更好我要做的事情的说明:


Ok, this doesn''t do what I was thinking about... it only restricts the paramter to be of type SplashScreenBase, not a surprise really...


What I really wanted to do is the following.
The parameter should be of type Type and should be assignable from SplashScreenBase...
So basically it should be a restricted parameter typeof(Type) to only contain certain Types.
Does that makes sense? I hope so ;)


UPDATE:
here is a better illustration of what I want to do:

public static SplashScreenManager Factory<T>(T splashScreenFormType, SplashScreenManagerOptions splashScreenManagerOptions) where T : Type [&& T is a Type that is derived from SplashScreenBase]



这段代码当然不能编译,但它应该显示目标是什么:)



能以某种方式完成吗?在约束文档中,我找不到任何可以让我归档我的目标......如果它是可行的......



安迪的想法非常感谢,

欢呼

Andy


This code of course does not compile, but it should show what the goal is :)

Can that be done somehow? In the constraints docs I couldn''t find anything that would let me archive my goal... if it is ever doable...

Andy thoughts are kindly appreciated,
cheers
Andy

推荐答案

我觉得你很接近。

类似于:

I think you''re close.
Something like:
public static SplashScreenManager Factory<T>(SplashScreenManagerOptions splashScreenManagerOptions) where T : SplashScreenBase



这将允许 T SplashScreenBase 的任何类型。

你只需要指定调用 Factory 时的实际类型:


This will allow T to be any type that is, or derives from, SplashScreenBase.
You would just need to specify the actual type when Factory was called:

var manager = SplashScreenManager.Factory<SplashScreenBase_Derived_Type>(options...);



您可能需要将 new 条件添加到其中,以便您可以创建实际的类实例。


You might need add the new condition to the where so you can create the actual class instance.


您可以尝试类似如何在C#中进行模板专业化 [ ^ ]。

干杯

Andi
You might try out something like How to do template specialization in C#[^].
Cheers
Andi


这篇关于将参数typeof(Type)限制为仅包含某些类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆