我该怎么办对象转换为字符串时,对象不是一个字符串?字符串、对象、我该、转换为

2023-09-06 08:51:29 作者:破碎的天堂鸟

-Edit-替代问题/例如http://stackoverflow.com/questions/2486873/how-do-i-cast-a-to-object-to-class-b-when-a-can-typcast-to-b

-Edit- Alternative question/example http://stackoverflow.com/questions/2486873/how-do-i-cast-a-to-object-to-class-b-when-a-can-typcast-to-b

我有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; }

我有code,这是否

I have code that does this

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 例外。所以,我要问:为什么你想通过在这种情况下字符串去?铸件一般是一个丑陋的屁股事,让你觉得也许我的设计需要重构一天。。他们是不是你设计到您的类库作为推荐的使用模式。他们用predictable行为低水平的设施,所以也没办法(并没有充分的理由提供一种方法)来覆盖什么是明确的转换一样。

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.

更新

这是您的评论来看,你是运行时多态性混合在一起,静态(编译时)的转换。他们不混得很清楚。你pviously $ P $的动态类型语言的用户?它好像你可能。如果你有一个方法:

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(); 
}

这将炸毁在编译时如果错误的对象是过去了,你不需要在运行时检查什么。少code,越早发现错误......是怎么整齐的呢?

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 对象,那么你可以把它在该接口上的属性:

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; }
}

或(如我前面提到的),你可以只覆盖的ToString 上的任何对象,因为这是一个虚拟方法在对象类,所有的类最终继承。所以说:

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.

在总结:

在虚拟和抽象方法,并支持接口:这些都是动态的,运行时类型和多态。 运算符和隐式转换超载(与仿制药):这些都是编译时,静态类型 石膏很恶心。