当对象不是字符串时,如何将对象强制转换为字符串? [英] How do i cast an object to a string when object is not a string?

查看:138
本文介绍了当对象不是字符串时,如何将对象强制转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-Edit-替代问题/示例当B可以向A打字时,如何投射A以反对A类?

-Edit- Alternative question/example How do i cast A to object to class A when B can typcast to A?

我有类A,B,C.它们都可以隐式转换为字符串

I have class A, B, C. They all can implicitly convert to a string

public static implicit operator A(string sz_) {  ... return sz; }

我有代码

object AClassWhichImplicitlyConvertsToString

{
    ...
    ((IKnownType)(String)AClassWhichImplicitlyConvertsToString).KnownFunc()
}

问题是,AClassWhichImplicitlyConvertsToString不是字符串,即使它可以被隐式转换为一个。我得到一个错误的转换异常。

The problem is, AClassWhichImplicitlyConvertsToString isnt a string even though it can be typecast into one implicitly. I get a bad cast exception. How do i say its ok as long as the class has an operator to convert into a string?

推荐答案

几乎肯定一种更好的方式来做任何你想做的事情。如果你提供更多的上下文,你会得到更多的有用的答案。

There is almost certainly a better way of doing whatever it is you're trying to do. If you provide more context, you'll get more helpful answers.

如果不是(或者)使你的类隐式转换为字符串,你也给他们 ToString 覆写,您可以说:

If instead of (or as well as) making your classes implicitly covert to string you also give them a ToString override, you can then say:

((KnownType)AClassBlah.ToString()).KnownFunc()

将一个字符串转换为 KnownType 。所以我不得不问:你为什么要在这种情况下通过 string Casts通常是一个丑陋的东西,让你认为也许我的设计需要重构一天。它们不是你设计到你的类库中作为推荐的使用模式。它们是一个具有可预测行为的低级设施,因此没有办法(没有提供方法的好理由)来重写显式转换的操作。

However, you'll then get an exception on trying to cast a string into KnownType. So I have to ask: why are you trying to go via string in this situation? Casts are generally an ugly-ass thing that make you think "Maybe my design needs refactoring one day". They're not something you design into your class library as a recommended usage pattern. They're a low-level facility with predictable behaviour, so there is no way (and no good reason to provide a way) to override what an explicit cast does.

更新

从您的评论中判断,您正在混合运行时多态性和静态(编译时)转换。他们不混合太好。您以前是动态类型语言的用户吗?看来你可能是。如果你有一个方法:

Judging from your comment you are mixing together runtime polymorphism and static (compile time) conversion. They don't mix too well. Are you previously a user of dynamically typed languages? It seems like you might be. If you have a method:

void FiddleWithObject(object obj)
{
    // whatever
}

然后,该方法的作者没有编译时知道什么操作可用 obj 。所以他们可以说:

Then the author of that method has no compile-time knowledge of what operations are available on obj. So they can say:

void FiddleWithObject(object obj)
{
    if (obj is IFiddly)
    {
        // Cool
        obj.Fiddle();
    }
    else
        throw new Exception("Wrong type of object");
}

然后在编译时会对不是 IFiddly 。但在静态类型语言中,您可以说:

This then blows up at compile time for classes that aren't IFiddly. But in a statically typed language, you can say:

void FiddleWithObject(IFiddly obj)
{
    obj.Fiddle(); 
}

如果传递错误的对象类型,并且你不需要在运行时检查任何东西。

This will blow up at compile time if the wrong kind of object is passed, and you don't need to check anything at runtime. Less code, bugs found sooner... how neat is that?

隐式转换功能是运算符重载特征集的一部分。这些都非常依赖于静态类型。它们在编译时基于对象的已知类型解析。所以如果你不知道一个对象的实际类,没有(内置)方法来调用它的操作符。

The implicit conversion feature is part of the operator overloading set of features. These are all very much tied to static types. They are resolved at compile time based on the known type of the object. So if you don't know the actual class of an object, there is no (built-in) way to call operators on it. It just doesn't mix with dynamic typing.

如果可以从 IFiddly中获取字符串(例如name) 对象,那么您可以将其作为该接口上的属性:

If it is possible to get a string (such as a "name") from an IFiddly object, then you can make it a property on that interface:

public interface IFiddly
{
    void Fiddle();
    string Name { get; }
}

或者(如前所述) c> ToString 对任何对象,因为 virtual 方法在对象所有类最终继承自。所以说:

Or (as I noted before) you could just override ToString on any object, as that's a virtual method on the object class that all classes ultimately inherit from. So by saying:

var str = someObject.ToString();

您将要调用 ToString someObject 中定义的实现是

You are going to be calling the ToString implementation defined in whatever class someObject is an instance of.

的实例。


  • 虚拟和抽象方法以及支持的接口:这些是用于动态,运行时类型和多态性。

  • 操作符和隐式转换重载和泛型):这些是用于编译时,静态类型。

  • 强制类型令人厌恶。

这篇关于当对象不是字符串时,如何将对象强制转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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