铸造在运行时多(未知类型) [英] casting to multiple (unknown types) at runtime

查看:74
本文介绍了铸造在运行时多(未知类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个浏览器,就可以打开所有我们通过我们的软件产生自定义文件。所有的文件从继承的IDocument,但我不知道如何去反序列化(一个好办法 - 嵌套的try / catch或许可以工作,但是这将是可怕的)。

I'm developing a viewer that will be able to open all of the custom documents that we produce via our software. All of the documents inherit from IDocument, but I'm not sure how to go about deserializing (in a good way - nested try/catch could probably work, but that would be hideous).

所以我的方法,因为它现在看起来是这样的:

So my method as it is now looks like this:

public Boolean OpenDocument(String filename, Type docType, out IDocument document)
{
    // exception handling etc. removed for brevity

    FileStream fs = null;
    BinaryFormatter bFormatter = new BinaryFormatter();

    fs = new FileStream(filename, FileMode.Open);
    document = (docType)bFormatter.Deserialize(fs);

    return true;
}



显然,这并不工作,我不能用变量的DOCTYPE这样,但我认为这说明了什么,我试图做的地步。什么是去有关的适当方式。

Obviously this doesn't work as I can't use the variable docType that way, but I think it illustrates the point of what I'm trying to do. What would be the proper way to go about that?

编辑> @约翰
好吧,也许我应该追加另一个问题:
如果我有接口:

edit> @John ok, maybe I should append another question: if I have an interface:

public interface IDocument
{
    public Int32 MyInt { get; }
}

和一个类:

public class SomeDocType : IDocument
{
    protected Int32 myInt = 0;
    public Int32 MyInt { get { return myint; } }

    public Int32 DerivedOnlyInt;
}

如果我反序列化到一个IDocument之外,将DerivedOnlyInt是对象的一部分 - 这样反序列化后,我可以转换为SomeDocType,它会被罚款

if I deserialize to an IDocument, will the DerivedOnlyInt be a part of the object - such that after deserializing, I can cast to SomeDocType and it'll be fine?

推荐答案

为什么不直接转换为<$? C $ C>的IDocument ?这样做有什么好处你相信铸造的确切类型会在这里?

Why not just cast to IDocument? What benefit do you believe casting to the exact type would have here?

如果你想调用者得到的结果在一个强类型的方式,这里就是我会写它,使用泛型:

If you want the caller to get the result in a strongly typed way, here's how I'd write it, using generics:

public T OpenDocument<T>(String filename) where T : IDocument
{
    using (FileStream fs = new FileStream(filename, FileMode.Open))
    {
        BinaryFormatter bFormatter = new BinaryFormatter();    
        return (T) bFormatter.Deserialize(fs);
    }
}



当然,这依赖于呼叫者知道正确请在编译时。如果他们不这样做,有没有办法,他们要懂得的使用的正确类型无论如何,所以只返回的IDocument

Of course, that relies on the caller knowing the right type at compile-time. If they don't, there's no way they're going to know how to use the right type anyway, so just return IDocument:

public IDocument OpenDocument(String filename)
{
    using (FileStream fs = new FileStream(filename, FileMode.Open))
    {
        BinaryFormatter bFormatter = new BinaryFormatter();    
        return (IDocument) bFormatter.Deserialize(fs);
    }
}



编辑:要回答这个编辑您的问题,是的派生的属性将仍然存在。铸造实际上不会更改对象 - 它只是意味着你得到该编译器知道是一个合适类型的引用

To answer the edit to your question, yes the derived property would still exist. Casting doesn't actually change an object - it just means you get a reference which the compiler knows is of an appropriate type.

这篇关于铸造在运行时多(未知类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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