Search code examples
c#radixref

C# cannot convert from x.ISessionContext to y.ISessionContext


I am having this strange error using base and ref. It says that I cannot convert from Consona.Business.ISessionContext to M2MDomain.ISessionContext at the word context in base.

//This class controls the functions in MolDatasheets
[BusinessObject("MolDatasheets")]
public class ExtendedMolDatasheets : BusinessComponent
{
    //This method creates a refrence to base 
    public ExtendedMolDatasheets(ref Consona.Business.ISessionContext context) : base(context)
    {
    }

I don't know why it will not convert when the same code will work in other files.


Solution

  • Because Consona.Business.ISessionContext and M2MDomain.ISessionContext are two separate classes. Just because they are named the same doesn't mean they are the same class. They reside in completely different namespaces, maybe completely different libraries.

    Unless there is an implicit conversion operator defined in the target class for the source class, you will get the error you have gotten.

    I'm not recommending an implicit conversion, by the way -- I would recommend defining a conversion function that you have to call explicitly. Your function would have to manually convert the first type to the second type however that makes sense in your application.