Runtime Instance From Mole Types

This is more of a quick tip than anything in regards to moles. When using moles, sometimes you might be find yourself executing a safe cast, or trying to, between your Mole and runtime instance type. Why would this happen? Well consider the following simple code:

[csharp]
TypeX x = ReturnAType() as TypeX;
[/csharp]

Now this is a problem with moles, because the typecast will fail resulting in x being null. However, the solution is just to stick with variant types when using Moled types, each moled type will have a property representing the Instance (the property is actually the MoledType.Instance property) which will expose the runtime instance. The cool part is the case is built automagically by the compiler :-)

Share

2 Comments

  1. Peli says:

    Each mole type has an implicit cast to the runtime type. That’s how the magic works.

  2. Peli says:

    Have you look into Behaviors yet?

Leave a Reply

Your email address will not be published. Required fields are marked *