简体   繁体   English

C#切换类的类型

[英]C# Switch For Types Of A Class

I have the following method which takes a type of class as a parameter: 我有以下方法,它将一种类作为参数:

public void test(Type proType){

}

I currently have a large if else which looks like: 我目前有一个大的if else看起来像:

if(proType == typeof(Class)){}

As there is about ten this looks untidy. 因为大约有十个看起来不整洁。

I tried turning this in to a switch but couldn't get it to work. 我尝试将其转换为开关但无法使其工作。

Is there any better practice to this or away to get a switch statement to work? 有没有更好的做法可以让这个或那个让switch语句工作?

           switch (proType)
            {
                case typeof(ClassName):
                    break;
            }

"A constant value required" “需要恒定值”

The function is been called like test(typeof(class)) 该函数被称为test(typeof(class))

So the aim is i have a Big object which contains many small classes. 所以目标是我有一个包含许多小类的Big对象。

The typeof(class) switch/if statement allows me to decide what container to go in to get the objects out. typeof(class)switch / if语句允许我决定进入哪个容器来获取对象。

So, how about making all the objects that you are testing share a common interface? 那么,如何让你正在测试的所有对象共享一个共同的界面呢?

 interface ITestable
 {
     void DoSomething();
 }

and each object implements this interface differently: 并且每个对象以不同方式实现此接口

 class MySomething : ITestable
 {
     public void DoSomething()
     {
         //type specific implementation
     }
 }

 class MyOtherSomething : ITestable
 {
     public void DoSomething()
     {
         //type specific implementation
     }
 }

Now: 现在:

 foreach(ITestable testable in myTestablesList)
 {
     testable.DoSomething();
 }

and all your switching logic disappears. 并且所有切换逻辑都消失了。 Tada! 田田!

What is it that you are really trying to achieve. 真正想要实现的目标是什么 I would guess that 9 out of 10 times, when you are switching over the type of some object, your design is flawed. 我猜这10次中的9次,当你切换某种物体的类型时,你的设计是有缺陷的。 Virtual dispatch or polymorphism (or both) are what you are really looking for in most of these cases, but without knowing what the problem is that you are trying to solve, one cannot say for certain. 在大多数情况下,虚拟调度或多态(或两者)都是您真正需要的,但不知道您试图解决的问题是什么,我不能肯定地说。

I normally go with a Dictionary to create a cache of actions to take for each type; 我通常使用Dictionary来为每种类型创建一个动作缓存; load it up at startup if types are known up front, or use TryGetValue and populate when it fails. 如果预先知道类型,则在启动时加载它,或者使用TryGetValue并在失败时填充。

You can use switch(Type.GetTypeCode(proType)) if the types you're interested in are in the TypeCode enum. 如果您感兴趣的类型在TypeCode枚举中,则可以使用switch(Type.GetTypeCode(proType)) For example, from the docs : 例如,来自文档

static void WriteObjectInfo(object testObject)
{
    TypeCode    typeCode = Type.GetTypeCode( testObject.GetType() );

    switch( typeCode )
    {
        case TypeCode.Boolean:
            Console.WriteLine("Boolean: {0}", testObject);
            break;

        case TypeCode.Double:
            Console.WriteLine("Double: {0}", testObject);
            break;

        default:
            Console.WriteLine("{0}: {1}", typeCode.ToString(), testObject);
            break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM