Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cast problem from class to interface

Tags:

c#

Assembly assembly = Assembly.LoadFrom("Logic\\bin\\Debug\\Logic.dll");
            Type queryManagerType = assembly.GetType("Logic." + HttpContext.Current.Session["lang_name"] + "SearchQueryManager");
            var queryManager = (ISearchQueryManager)Activator.CreateInstance(queryManagerType);


public interface ISearchQueryManager
    {
        IList<Advertisements> ApplyQueries(string searchQuery, int page, int pageSize, string orderBy, out int count);
    }

public class SlovenianSearchQueryManager : ISearchQueryManager
    {
...
}

but i get

Unable to cast object of type 'Logic.SlovenianSearchQueryManager' to type 'Logic.ISearchQueryManager'.

EDIT: whole stacktrace

System.InvalidCastException was unhandled by user code
Message="Unable to cast object of type 'Logic.SlovenianSearchQueryManager' to type 'Logic.ISearchQueryManager'."
Source="ViaMura.Web.Module"
StackTrace: at ViaMura.Web.Module.WebController.GetAdvertismentsByRawQuery(String rawQuery, Int32 page, Int32 pageSize, String orderBy, Int32& count) in D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web.Module\WebController.cs:line 32 at ViaMura.Web.Module.Views.SearchResultsPresenter.OnResultsLoad(Int32 page, Int32 pageSize, String orderBy) in D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web.Module\Views\SearchResultsPresenter.cs:line 43 at ViaMura.Web.SearchResults.SearchAdvertisments() in D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\SearchResults.aspx.cs:line 155 at ViaMura.Web.SearchResults.Page_Load(Object sender, EventArgs e) in D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\SearchResults.aspx.cs:line 149 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at ViaMura.Web.App_Code.PageControllers.BasePage.OnLoad(EventArgs e) in D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\App_Code\PageControllers\BasePage.cs:line 89 at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:

EDIT2:

string a1 = typeof (ISearchQueryManager).Assembly.Location;
string a2 = typeof(SlovenianSearchQueryManager).Assembly.Location

give me the same result:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\5438a399\53975f83\assembly\dl3\0f9540b5\15407fe2_5db7cb01\Logic.DLL

but

string a3 = queryManagerType.Assembly.Location;

gives me different path:

D:\PROJEKTI\crawler\WebCrawlerSuite\WebCrawler.Logic\bin\Debug\WebCrawler.Logic.dll

like image 220
senzacionale Avatar asked Jan 18 '11 22:01

senzacionale


People also ask

Can you cast a class to an interface?

Yes, you can. If you implement an interface and provide body to its methods from a class. You can hold object of the that class using the reference variable of the interface i.e. cast an object reference to an interface reference.

Can you cast to an interface C#?

You can access the members of an interface through an object of any class that implements the interface. For example, because Document implements IStorable , you can access the IStorable methods and property through any Document instance: Document doc = new Document("Test Document"); doc.

What is casting how it is used in interface?

A type cast—or simply a cast— is an explicit indication to convert a value from one data type to another compatible data type. A Java interface contains publicly defined constants and the headers of public methods that a class can define.


2 Answers

In my experience, type mismatch problems like this are always caused by loading types from two different locations, even if you think they should be getting loaded from the same place.

Compare: Two Types not equal that should be

Try looking at the Assembly.Location property of each type in the debugger:

typeof(ISearchQueryManager).Assembly.Location
typeof(SlovenianSearchQueryManager).Assembly.Location
like image 176
Jorgen Thelin Avatar answered Sep 20 '22 12:09

Jorgen Thelin


One possible reason for this happening is that you have this ISearchQueryManager interface defined in two different assemblies which in fact doesn't represent the same type. I see that you are playing with dynamic assembly loading. So the interface you are statically casting to is not the interface implemented by the SlovenianSearchQueryManager class even if it has the same name.

like image 35
Darin Dimitrov Avatar answered Sep 20 '22 12:09

Darin Dimitrov