简体   繁体   English

c# 中的类型铸造问题

[英]Trouble with type casting in c#

I have 2 classes:我有 2 节课:

class A {
    public void A_1(Object b) {
        ...
        Type t = b.GetType();
        (t.FullName)b.B_1(); //It doesn`t work! Error in cast
    }
    ....
}

class B {
    public void B_1() {
        ...
    }
    ....
}

A a = new A();
B b = new B();
a.A1(b);

How to cast object correctly?如何正确投射 object?

If you want to cast an object of any type to an object of another type, you do this:如果要将任何类型的 object 转换为另一种类型的 object,请执行以下操作:

// Will Throw an exception at runtime if it cant be cast.
B newObject = (B)oldObject; 

// Will return null at runtime if the object cannot be cast
B newObject = oldObject as B; 

// If in a generic method, convert based on the generic type parameter regardless of condition - will throw an exception at runtime if it cant be cast
B newObject = (T)Convert.ChangeType(oldObject, typeof(T))

Your syntax is off;您的语法已关闭; you don't convert from the fullname to the object, you simply convert from the type symbol.您不会从全名转换为 object,您只需从类型符号转换。

double x = (double)40;

ClassB anotherInstance = (ClassB)someOtherInstance;

What you're trying to do is basically:你想要做的基本上是:

Foo myFoo = ("Foo")myObject;

That definitely will not work in C#.这绝对不适用于 C#。 When you cast in C#, the compiler emits code that does the cast, it needs to know what it's casting from and to, in order to write that code.当您在 C# 中进行转换时,编译器会发出执行转换的代码,它需要知道它从什么转换到什么,才能编写该代码。 A string does not help the compiler out here.字符串在这里对编译器没有帮助。

As others have pointed out, what you want to do doesn't seem like you really need to (unless this is just a contrived example).正如其他人所指出的那样,您想要做的似乎并不是您真正需要的(除非这只是一个人为的例子)。 If you really want to do this, you'll need to work with a more dynamic language than C#, or find a C# friendly way of accomplishing this.如果你真的想这样做,你需要使用比 C# 更动态的语言,或者找到一种 C# 友好的方式来实现这一点。

Are you sure you didn't mean to do (B)b.B_1() ?你确定你不是故意的(B)b.B_1()吗?

C# has a static type-system, ie all types must be known at compile-time (modulo reflection). C# 有一个 static 类型系统,即在编译时必须知道所有类型(模反射)。 So, casting to a type that is only known at run-time makes no sense.因此,强制转换为仅在运行时已知的类型是没有意义的。 Specify the type explicitly:明确指定类型:

public void A_1(object obj)
{
    ...
    B b = (B)obj;
    b.B_1();
    // or
    ((B)obj).B_1();
}

You can also do this:你也可以这样做:

class A {
    public void A_1(Object b) {
        ...
        if (b is B)
        {
             ((B)b).B_1();
        }
    }
    ....
}

Type.FullName is just a string; Type.FullName只是一个字符串; it's not a type.它不是一种类型。 Use this instead: ((B)b).B_1();改用这个: ((B)b).B_1(); Also, using GetType() is a way to get the type of an object dynamically, but casting is only possible or useful when the target type is known at compile time (not dynamic at all).此外,使用GetType()是一种动态获取 object 类型的方法,但只有在编译时知道目标类型(根本不是动态的)时,转换才可能或有用。 In order to cast, simply refer to the type directly in a pair of parentheses.为了进行强制转换,只需在一对括号中直接引用类型即可。 Don't attempt to obtain or use an object of type Type .不要尝试获取或使用Type的 object。

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

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