use*_*964 143 .net c# reflection case switch-statement
可能重复:
C# - "开启类型"有比这更好的选择吗?
如果你想要switch一种类型的对象,最好的方法是什么?
private int GetNodeType(NodeDTO node)
{
switch (node.GetType())
{
case typeof(CasusNodeDTO):
return 1;
case typeof(BucketNodeDTO):
return 3;
case typeof(BranchNodeDTO):
return 0;
case typeof(LeafNodeDTO):
return 2;
default:
return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这不起作用,但我想知道如何解决这个问题.if/else在这种情况下是否适用声明?
或者你使用开关并添加.ToString()到类型?
Ash*_*ley 111
这不会直接解决您的问题,因为您想要打开自己的用户定义类型,但为了其他只想打开内置类型的人的利益,您可以使用TypeCode枚举:
switch (Type.GetTypeCode(node.GetType()))
{
case TypeCode.Decimal:
// Handle Decimal
break;
case TypeCode.Int32:
// Handle Int32
break;
...
}
Run Code Online (Sandbox Code Playgroud)
Arn*_*psa 42
在MSDN博客文章中的许多问题:开启类型是关于为什么.NET不提供切换类型的一些信息.
像往常一样 - 变通办法始终存在.
这个不是我的,但不幸的是我失去了源头.它使得切换类型成为可能,但我个人认为它很尴尬(字典的想法更好):
public class Switch
{
public Switch(Object o)
{
Object = o;
}
public Object Object { get; private set; }
}
/// <summary>
/// Extensions, because otherwise casing fails on Switch==null
/// </summary>
public static class SwitchExtensions
{
public static Switch Case<T>(this Switch s, Action<T> a)
where T : class
{
return Case(s, o => true, a, false);
}
public static Switch Case<T>(this Switch s, Action<T> a,
bool fallThrough) where T : class
{
return Case(s, o => true, a, fallThrough);
}
public static Switch Case<T>(this Switch s,
Func<T, bool> c, Action<T> a) where T : class
{
return Case(s, c, a, false);
}
public static Switch Case<T>(this Switch s,
Func<T, bool> c, Action<T> a, bool fallThrough) where T : class
{
if (s == null)
{
return null;
}
T t = s.Object as T;
if (t != null)
{
if (c(t))
{
a(t);
return fallThrough ? s : null;
}
}
return s;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
new Switch(foo)
.Case<Fizz>
(action => { doingSomething = FirstMethodCall(); })
.Case<Buzz>
(action => { return false; })
Run Code Online (Sandbox Code Playgroud)
bja*_*jax 25
我遇到了同样的问题并且发现了这篇文章.这是IDictionary方法的意思:
Dictionary<Type, int> typeDict = new Dictionary<Type, int>
{
{typeof(int),0},
{typeof(string),1},
{typeof(MyClass),2}
};
void Foo(object o)
{
switch (typeDict[o.GetType()])
{
case 0:
Print("I'm a number.");
break;
case 1:
Print("I'm a text.");
break;
case 2:
Print("I'm classy.");
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
如果是这样,我不能说我喜欢将字典中的数字与case语句进行协调.
这将是理想的,但字典引用会杀死它:
void FantasyFoo(object o)
{
switch (typeDict[o.GetType()])
{
case typeDict[typeof(int)]:
Print("I'm a number.");
break;
case typeDict[typeof(string)]:
Print("I'm a text.");
break;
case typeDict[typeof(MyClass)]:
Print("I'm classy.");
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
还有其他我忽略的实现吗?
Dav*_*ier 24
我只是使用if语句.在这种情况下:
Type nodeType = node.GetType();
if (nodeType == typeof(CasusNodeDTO))
{
}
else ...
Run Code Online (Sandbox Code Playgroud)
另一种方法是:
if (node is CasusNodeDTO)
{
}
else ...
Run Code Online (Sandbox Code Playgroud)
第一个示例仅适用于确切类型,后者也检查继承.
Dav*_*nde 12
你可以这样做:
if (node is CasusNodeDTO)
{
...
}
else if (node is BucketNodeDTO)
{
...
}
...
Run Code Online (Sandbox Code Playgroud)
虽然那会更优雅,但它可能没有其他一些答案那么高效.
小智 10
你可以这样做:
function void PrintType(Type t) {
var t = true;
new Dictionary<Type, Action>{
{typeof(bool), () => Console.WriteLine("bool")},
{typeof(int), () => Console.WriteLine("int")}
}[t.GetType()]();
}
Run Code Online (Sandbox Code Playgroud)
它很清楚,很容易.它比在某处缓存字典慢一点..但是对于很多代码来说这无关紧要..
| 归档时间: |
|
| 查看次数: |
199148 次 |
| 最近记录: |