仅允许将特定类型用作方法参数 [英] Allow only specific types as method parameter

查看:83
本文介绍了仅允许将特定类型用作方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下方法:

Hi,

I''ve got the following method:

private void CreateSomething(Type pType)
{
    if (pType.BaseType != typeof(Window))
        throw new Exception("Wrong type.");
           
    ...
}


因为我只想使用从Window继承的类型调用该方法,所以如果pType的基本类型不是Window,则会引发异常.我认为这种解决方案不是很好.

是否可以通过仅允许特定类型(分别继承给定Type的继承类型)的方法来更改方法的签名?


谢谢!
Gobble-G


As I only want to call the method with types inherited from Window, an exception is thrown if the base type of pType isn''t Window. In my opinion this solution isn''t very nice.

Is there a possibility to change the signature of the method in that way that only specific types respectively inherited types of a given Type are allowed to call the method?


Thanks!
Gobble-G

推荐答案

我看到您希望有可能是从Type
派生的类 例如class WindowType:类型{},可用于为您的方法提供一些静态类型安全性,但不幸的是没有.


...这不是完全正确的. Type是抽象的-编译器(或代码生成器)为程序定义的每种新类型(classstructenum ...)创建一个实现(Type的派生).应用程序无法显式访问派生,类似于匿名类型或匿名委托或lambda的类型.

您可以按照以下要求强制执行编译类型安全性:
I can see that you are hoping that there may be classes derived from Type
e.g. class WindowType : Type {} which could be used to provide some static type safety for your method but unfortunately there are not.


... This is not entirely true. Type is abstract - the compiler (or code generator) creates a implementation (a derivate of Type for each new type (class, struct, enum,...) defined by the program. These derivates are not explicitely accessible by the application, similar to anonymous types or types of anonymous delegates or lambdas.

You can enforce the compile type safety you require as follows:
// CreateSomething is poorly chosen name for something
// which returns ''void''
private void CreateSomething<AllowedType>() where AllowedType : System.Windows.Window 
{
 // no need to throw or check since compiler will refuse
 // to compile for any other call then CreateSomething<Window>
 // or any of its derivates

 // do your stuff
}



客户端需要定义执行操作的类型:



The client needs to define type for which the operation should be performed:

CreateSomething<System.Windows.Window>(); // ok

CreateSomething<System.Windows.NavigationWindow>(); // ok

CreateSomething<System.String>(); // compiler error!



观察到您根本不需要调用GetTypetypeof,因为编译器甚至都不会让您走得那么远.



Observe that you don''t need to invoke GetType or typeof at all since the compiler does not even let you get that far.


我们称之为多态性(如果我得到了您):
We call it polymorphism (if I got you):
private void CreateSomething(Window derivedFromWindow)
{
  //..
}


:-)


类型对象基于与该对象关联的元数据表示实例的运行时类型编译时以限制可能分配给您的方法的参数.

我可以看到您希望有可能是从Type
派生的类 例如class WindowType : Type {}可以用来为您的方法提供一些静态类型安全性,但不幸的是没有.

艾伦.
A Type object represents the run-time type of an instance based on the metadata associated with the object and there is nothing that can be done at compile-time to restrict the argument that may be assigned to your method.

I can see that you are hoping that there may be classes derived from Type
e.g. class WindowType : Type {} which could be used to provide some static type safety for your method but unfortunately there are not.

Alan.


这篇关于仅允许将特定类型用作方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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