简体   繁体   English

将变量强制转换为另一个Type变量表示的类型?

[英]Cast a variable to a type represented by another Type variable?

I'm aware that questions like this have been asked before and I doubt it's possible, but I just wanted to make 100% sure that it isn't. 我知道以前曾问过这样的问题,我怀疑是否有可能,但我只是想100%地确定不可能。

In VB.net or C# (either language, it doesn't matter), I want to cast a variable to a type represented by another Type variable. 在VB.net或C#(两种语言都没关系)中,我想将变量转换为由另一个Type变量表示的类型。 Here's an example of the kind of code that would be needed in C#: 这是C#中需要的那种代码的示例:

Object castMe = new Foo();
Type castTo = typeof(Foo);
Foo beenCast = (castTo.GetRepresentedType())castMe;
beenCast.MethodInFoo();

... or in VB, something like: ...或在VB中,例如:

Dim castMe As Object = New Foo()
Dim castTo As Type = GetType(Foo)
Dim beenCast As Foo = CType(castMe, castTo.GetRepresentedType())
beenCast.MethodInFoo()

The big problem is obviously specifying a method which will return a Type at runtime for the cast type argument, rather than an actual compile-time type (ie. CType(castMe, Foo) ). 显然,最大的问题是指定一种方法,该方法将在运行时为CType(castMe, Foo)类型参数返回Type,而不是实际的编译时类型(即CType(castMe, Foo) )。 I don't quite understand why you can't do this, though... sure, runtime cast errors may result, but you can also get them when you DO specify a compile-time type. 我不太明白为什么不能这样做,但是...肯定会导致运行时强制转换错误,但是当您指定编译时类型时也可以得到它们。 If castMe is not an instance of Foo, then even CType(castMe, Foo) will still throw an InvalidCastException . 如果castMe不是Foo的实例,那么即使CType(castMe, Foo)仍将抛出InvalidCastException

If you do know to which type you want to cast you will have to end up with something like this: 如果您确实知道要转换为哪种类型,则必须以如下形式结束:

public static T Cast<T>(object o) {
  return (T)o;
}

T is now your type argument. T现在是您的类型参数。

If you don't know what the type is that you cast to, the compiler can't know either. 如果您知道要转换为什么类型,则编译器也不知道。 That's a statically typed language for you. 这是适合您的静态类型语言。 If you don't like such things you may want to have a wander through eg ruby. 如果您不喜欢这样的东西,您可能想要在例如红宝石中徘徊。

In cases like that, usual responses are abstactions via base classes or interfaces - extract common behaviour into properties, events and methods that can be used in a statically typed language, even though you don't know the specific type in a given situation. 在这种情况下,通常的响应是通过基类或接口进行的抽象操作-将常见行为提取到可以在静态类型的语言中使用的属性,事件和方法中,即使您在给定情况下不知道特定的类型。

Since you will ultimately be assigning the cast to a 'Foo', you can just write 由于您最终会将演员表分配给“ Foo”,因此您可以编写

Foo beenCast = (Foo)castMe; Foo beCast =(Foo)castMe;

If 'castMe' is any other type than 'Foo' (or derived), the cast will fail anyway, it does matter what you are trying to cast it to. 如果“ castMe”不是“ Foo”(或派生的)以外的任何其他类型,则强制转换将始终失败,这与您尝试将其强制转换为何种类型无关。

Thanks to Frank, I've been able to get the function I wanted to work, working. 多亏了弗兰克(Frank),我才能够获得想要工作的功能。 The function I ended up writing was: 我最终编写的函数是:

Public Shared Function CastTypeOrNull(Of T)(ByVal castMe As Object) As T
    If (castMe Is Nothing Or castMe.GetType Is GetType(System.DBNull)) Then
        Return Nothing
    Else
        Return CType(castMe, T)
    End If
End Function

... and I call it using: ...而我使用以下方式调用它:

varDateNullable = CastTypeOrNull(Of Nullable(Of Date))(row("DateTimeValue"))

Obviously you need to know the type you want to case to at compile time (I do), but it's a really nice shorthand to replace having to write a separate method for each different type you might want to cast to. 显然,您需要在编译时就知道要实例化的类型(我确实知道),但是这是一个非常好的速记方式,可以替换为每个可能要转换为的不同类型编写单独的方法。

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

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