如何将类加载到类型中? “不能将'x'类型的对象强制转换为'x'" [英] How Do I Load A Class Into A Type? "Cannot cast object of type 'x' to 'x'"

查看:105
本文介绍了如何将类加载到类型中? “不能将'x'类型的对象强制转换为'x'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





所以,我的代码一直运行正常。我有一个状态安全的工作流程,这意味着如果工作流引擎停止和卸载,那么它应该能够从我离开的地方开始。



有执行某些设置的某些阶段(例如订阅事件),如果工作流程在稍后阶段加载,我需要确保完成。



For示例:

Hi,

So, my code has been working ok. I have a state-safe workflow, which means that if the workflow engine stops and unloads, then it should be able to start back where I left it.

There are some stages that perform some setup (such as subscribing to events for eg) which I need to make sure is done if the workflow is loaded in a later stage.

For example:

Tracker Workflow Starts:
   state1: setup (subscribe to events)
   state2: wait for event (requires subscription to event)
   state3: report back to parent workflow





如果工作流程在第2阶段停止(关闭/崩溃),那么我需要重新订阅该事件。



我向一个阶段的基类添加了一个方法,该方法仅在工作流从关闭状态重新加载时运行。我可以在state2中重写它并重新订阅。



如果我没有关闭工作流程,下面的代码运行正常:



if the workflow is stopped (shutdown / crash) at stage 2, then I need to resubscribe to the event.

I added a method to the baseclass of the stages that runs only if the workflow is reloaded from a shutdown state. I can override it in state2 and resubscribe.

If I do not shut down the workflow, the following code runs fine:

public static IWorkflow LoadWorkflow(Workflow workflowInfo)
{
    Type type = LoadWorkflowType(workflowInfo);

    MethodInfo method = type.GetMethod("DeSerialize", Flags);
    if (method == null && type.BaseType != null)
        method = type.BaseType.GetMethod("DeSerialize", Flags | BindingFlags.InvokeMethod).MakeGenericMethod(type);

    if (method == null)
        throw new NullReferenceException("No DeSerialize method found!");

    IWorkflow workflow = (IWorkflow)method.Invoke(type, new object[] { workflowInfo });

    return workflow;
}





如果我重新启动它,那么我在调用时遇到错误:



If I do restart it then I get an error at the invocation:

Quote:

'WorkFlowEngine.Projections.Workflow'类型的对象无法转换为'WorkFlowEngine.Projections.Workflow'类型。

Object of type 'WorkFlowEngine.Projections.Workflow' cannot be converted to type 'WorkFlowEngine.Projections.Workflow'.



认为这是因为在运行时加载了一个版本的程序集( Type type ) ,而另一个是直接参考(输入参数 Deserialize )。



i想要将加载的类型转换为'WorkFlowEngine.Projections.Workflow'的引用版本。



甚至可以吗?

我有更好的办法吗?





谢谢^ _ ^

Andy


I think that this is because one version of the assembly is loaded at runtime (Type type), whilst the other is a direct reference (the input parameter of "Deserialize").

i would like to convert the loaded version of type to the referenced version of 'WorkFlowEngine.Projections.Workflow'.

Is that even possible?
Is there a better way I can do this?


Thanks ^_^
Andy

推荐答案

解决了 - 男人,这是一个痛苦!



您需要确保调用的所有内容的基本类型是从不同的库引用到加载的程序集。



这只意味着重新洗牌一半代码> _<< br mode =hold/>



所以:

如果你的加载程序集是基本类型程序集,那么它已经被加载到appdomain中。

如果你的基类在加载程序集然后它将由appdomain作为引用加载一次,并再次作为加载的程序集加载。这意味着程序集不是同一个实例,虽然它们是相同的,但它们永远不会匹配。
Solved - Man, this is a pain!

You need to make sure that the base types for EVERYTHING you invoke is reference from a different library to the loaded assemblies.

It only means re-shuffling half you code >_<<br mode="hold" />

So:
If your loading assembly is the base type assembly then it will already be loaded into the appdomain.
If your base class is in the loaded assembly then it will be loaded once by the appdomain as a reference and again as the loaded assembly. This means that the assemblies are not the same instance, and although they are identical, they will never match.


这篇关于如何将类加载到类型中? “不能将'x'类型的对象强制转换为'x'&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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