gpt4 book ai didi

c# - 将问题从类转移到接口(interface)

转载 作者:太空狗 更新时间:2023-10-29 21:10:52 27 4
gpt4 key购买 nike

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
{
...
}

但是我明白了

无法将“Logic.SlovenianSearchQueryManager”类型的对象转换为类型“Logic.ISearchQueryManager”。

编辑:整个堆栈跟踪

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:

编辑2:

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

给我同样的结果:

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

但是

string a3 = queryManagerType.Assembly.Location;

给我不同的路径:

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

最佳答案

根据我的经验,像这样的类型不匹配问题总是由从两个不同位置加载类型引起的,即使您认为它们应该从同一个位置加载也是如此。

比较:Two Types not equal that should be

尝试在调试器中查看每种类型的 Assembly.Location 属性:

typeof(ISearchQueryManager).Assembly.Location
typeof(SlovenianSearchQueryManager).Assembly.Location

关于c# - 将问题从类转移到接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4729532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com