我遇到了一个问题,我不确定是否能解决这个问题。我的库和另一个库都为特定类型的数据提供了API,现在我已经编写了一个interop库,它可以在这两个库之间自由地转换数据,这很好用。
但我现在已经尝试编写一个小的演示应用程序,使用依赖于其他API的第三个库通过我的API访问数据,这就是我遇到问题的地方。第三个库引用其他API的合并版本和一些要引导的相关DLL,这些DLL是其他API的旧版本,而我的API以其独立形式引用其他API的最新版本。因此,首先,我必须使用外部别名来编写与这3个库一起编写的代码,否则类型会发生冲突,并且无法编译。
因此,现在当我尝试编写一个应用程序,它从我的API中获取数据,通过互操作层运行它,因此它被转换成另一个API,然后使用第三个库来处理这些数据,因为.Net不会在类型之间进行转换,因为它认为它们是不同的。有没有办法迫使.Net这么做,或者我不走运?
据我所知,第三个库的作者计划在将来将他的库迁移到另一个API的最新版本(1.0.6.4),所以我可能只需要等到那个时候?
using System;
using System.Collections.Generic;
using System.Linq;
using VDS.RDF;
using VDS.RDF.Interop;
using VDS.RDF.Parsing;
using VDS.RDF.Storage;
using SemWeb;
using LinqToRdf;
namespace RdfMusic
{
public class Program
{
public static void Main(string[] args)
{
try
{
//Get the Talis Connection
TalisPlatformConnector talis = new TalisPlatformConnector("store", "user", "password");
NativeStoreSource source = new NativeStoreSource(talis);
//Load the Data
Graph g = new Graph();
FileLoader.Load(g, "data.ttl");
//Ensure the data is in Talis
talis.SaveGraph(g);
//Now do a LinqToRdf query
//BUG: This cast fails since it's trying to cast to StatementSource in
//the SemWeb library (version 1.0.5.0 as referenced by LinqToRdf) from
//a StatementSource in the SemWeb library (version 1.0.6.4 as referenced by dotNetRDF)
StatementSource stmtSource = (StatementSource)source;
LinqToRdf.TripleStore ts = new LinqToRdf.TripleStore(new Store(stmtSource));
ts.QueryType = QueryType.LocalN3StoreInMemory;
IEnumerable<Track> tracks = from t in new MusicDataContext(ts).ForType<Track>()
where t.Year == "2006"
select t;
foreach (Track t in tracks)
{
Console.Write(t.Title + " by " + t.ArtistName);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
Exception innerEx = ex.InnerException;
while (innerEx != null)
{
Console.WriteLine();
Console.WriteLine(innerEx.Message);
Console.WriteLine(innerEx.StackTrace);
innerEx = innerEx.InnerException;
}
}
Console.WriteLine();
Console.WriteLine("Press Enter to exit");
Console.ReadLine();
}
}
}发布于 2010-02-16 00:07:09
是的,你真不走运。从CLR的角度来看,类型名称是程序集名+可选模块名+类型名(这包括命名空间和外部类),并且系统是名义的,而不是结构化的-不同的名称意味着不同的类型。你能做的最好的事情就是手动转换,只要你能给出成员的可访问性(或者只是使用反射来解决它)。
发布于 2016-03-31 14:57:58
在这种情况下,您可以使用Automapper来简化类型之间的映射。
https://stackoverflow.com/questions/2267104
复制相似问题