【C#】VS根据系统自动更改引用64和32位的动态库(转载)

转自http://blog.youkuaiyun.com/duan425/article/details/52277181
大多数项目都需要编译32位和64位两种版本
VS中在配置管理器中可以直接选择X86以及X64两种模式分别生成对应版本的项目文件
但是项目引用的dll库有时候会有x64和x86两种格式,需要在生成两种版本时候加载不同的dll文件。
 
这里我遇到的情况是加载System.Data.SQLite.dll的数据库文件,会有区分x86和x64两种。之前切换生成64位和32位版的时候,都要删除引用并重新添加引用,非常麻烦。这里介绍一种方法可以设置一次,不需要每次都修改引用dll的路径。
 
首先找到解决方案中加载System.Data.SQLite.dll库的工程。找到对应的 工程名.csproj。使用文本编辑器打开 这个文件 ,这是一个使用xml方式的工程文件 。找到加载库的地方
如:
[html]  view plain  copy
 
  1. <Reference Include="System.Data.SQLite">  
  2.     <SpecificVersion>False</SpecificVersion>  
  3.     <HintPath>lib\SQLite-64\System.Data.SQLite.dll</HintPath>  
  4. </Reference>  
添加Condition选项 如:
[html]  view plain  copy
 
  1. <Reference Condition=" '$(Platform)' == 'AnyCPU'" Include="System.Data.SQLite">  
  2.   <SpecificVersion>False</SpecificVersion>  
  3.   <HintPath>lib\SQLite-86\System.Data.SQLite.dll</HintPath>  
  4. </Reference>  
  5. <Reference Condition=" '$(Platform)' == 'x64'" Include="System.Data.SQLite">  
  6.   <SpecificVersion>False</SpecificVersion>  
  7.   <HintPath>lib\SQLite-64\System.Data.SQLite.dll</HintPath>  
  8. </Reference>  
 
这样便可以实现切换x86和x64自动切换加载动态库路径
 
 
还有一种场景是这样的:
解决方案中一个工程生成dll,另一个工程加载此工程生成的dll
除了这个工程生成x86以及x64两个版本之外,还会有debug版和release版两种情况。这里的解决方案就比较简单了
原先是这样的
[html]  view plain  copy
 
  1.    <Reference Condition=" '$(Platform)' == 'x86'" Include="ConvertTasks, Version=1.0.0.0, Culture=neutral, processorArchitecture=x86">  
  2.      <SpecificVersion>False</SpecificVersion>  
  3.      <HintPath>ConvertTaskManager\ConvertTaskManager\bin\Release\ConvertTasks.dll</HintPath>  
  4.    </Reference>  
  5. <Reference Condition=" '$(Platform)' == 'x64'" Include="ConvertTasks, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">  
  6.      <SpecificVersion>False</SpecificVersion>  
  7.      <HintPath>ConvertTaskManager\ConvertTaskManager\bin\x64\Release\ConvertTasks.dll</HintPath>  
  8.    </Reference>  
只需要将路径中debug或release的目录替换成$(Configuration)即可
如下:
[html]  view plain  copy
 
  1.    <Reference Condition=" '$(Platform)' == 'x86'" Include="ConvertTasks, Version=1.0.0.0, Culture=neutral, processorArchitecture=x86">  
  2.      <SpecificVersion>False</SpecificVersion>  
  3.      <HintPath>ConvertTaskManager\ConvertTaskManager\bin\$(Configuration)\ConvertTasks.dll</HintPath>  
  4.    </Reference>  
  5. <Reference Condition=" '$(Platform)' == 'x64'" Include="ConvertTasks, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">  
  6.      <SpecificVersion>False</SpecificVersion>  
  7.      <HintPath>ConvertTaskManager\ConvertTaskManager\bin\x64\$(Configuration)\ConvertTasks.dll</HintPath>  
  8.    </Reference>  
<think>Hmm,theuserisaskingabouttheimplementationprinciplesandcodeanalysisofusingtheAdaptextensionmethodinC#toconvertaList<Animal>toList<Bear>.Fromthereferencematerialtheyprovided,itseemsthey'veencounteredaspecificcodesnippetdemonstratingthisconversionusingtheMapsterlibrary.Lookingatthecontext,theuserappearstobeanintermediatetoadvancedC#developerworkingwithobjectmappingandtypeconversion.They'relikelydealingwithareal-worldscenarioinvolvingDTOtransformationsorsimilarchallenges.Thereferenceshowsthey'vealreadyseenanexamplebutwantdeeperunderstanding.Thekeythingstoaddressare:thecoremechanismbehindMapster'stypemapping,howextensionmethodsenablefluentsyntax,theruntimevscompile-timeimplications,andthedifferencesbetweenshallowcopyingandtrueobjectconversion.TheusermightalsobenefitbyknowingwhythisapproachispreferabletotraditionalmethodslikeSelect()projections.ThereferenceusesList<Bear>bears=newList<Bear>();butdoesn'tassigntheconvertedresult,whichisinteresting-probablyjustdemocode.Ishouldexplainwhytheactualconversionhappensinvartmp=animals.Adapt<List<Bear>>().TheFilterexampleseemsunrelatedtotheirimmediatequestionbutshowsthey'reworkingwithcollectionviews.NeedtocarefullydistinguishthatAnimalandBearareseparatetypes-possiblynotinaninheritancehierarchy-sothisdemonstratestrueobject-to-objectmapping,notjustcasting.Shouldwarnaboutcommonpitfallslikemissingtypeconfiguration.Mightbeworthbrieflymentioningperformancesincetheyshowedfilteringintheirsample.Theuserdidn'texplicitlyaskaboutconfigurationbutthenullDescriptionintheoutputsuggeststheymightrunintomappingissues-betterincludeconfigexamplesproactively.Shouldstructuretheexplanationfromcoreconcepttopracticalusage,thenedgecases.</think> 以下是基于C#中Mapster库的`Adapt`扩展方法的实现原理代码解析: ### 一、实现原理 1. **泛型对象映射** `Adapt`方法通过动态类型映射实现集合转换,其核心流程: ```csharp public static TDestination Adapt<TDestination>(this object source) { var mapper = TypeAdapterConfig.GlobalSettings.GetMapFunction(source.GetType(), typeof(TDestination)); return (TDestination)mapper(source); } ``` - 利用运行时泛型类型推断(`GetType()``typeof(TDestination)`) - 通过全局配置`TypeAdapterConfig`获取映射函数 - 执行类型转换委托生成新对象[^1] 2. **集合转换机制** - 遍历源集合`List<Animal>` - 对每个元素执行`Animal -> Bear`的类型映射 - 自动创建目标集合`List<Bear>` - **本质是浅拷贝**:仅复制属性值,不改变引用地址 3. **与直接转换的区别** | 方式 | 类型安全 | 需继承关系 | 字段映射 | |---|---|----|---| | `Adapt()` | 是 | 否 | 自动同名属性匹配 | | `(Bear)animal` | 否 | 必需 | 无 | ### 二、代码解析(用户示例) ```csharp List<Animal> animals = new List<Animal>() { new Animal(1,"ani1"), new Animal(2,"动物2") }; // 核心转换操作(生成新集合) var tmp = animals.Adapt<List<Bear>>(); // 修改新集合元素属性 tmp.ForEach(m => m.Description = "Animal adapt bear..."); // 应用过滤(演示转换后操作) ListCollectionView view = new ListCollectionView(tmp); view.Filter = i => ((Bear)i).ID == 2; // 仅保留ID=2的元素 ``` ### 三、关键点说明 1. **字段映射规则** - 自动匹配同名属性(如`ID`/`Name`) - 若`Animal``Bear`有同名属性,自动复制值 ```csharp // 转换结果等价于: new Bear { ID = animal.ID, Name = animal.Name } ``` 2. **类型配置要求** - 不同名属性需预先配置映射规则: ```csharp TypeAdapterConfig<Animal, Bear>.NewConfig() .Map(dest => dest.FullName, src => src.Name); ``` 3. **转换局限性** - 不支持构造函数参数映射 - 嵌套对象需额外配置 - **运行时转换失败**示例: ```csharp // 若Bear有Animal不存在的Required属性 var tmp = animals.Adapt<List<Bear>>(); // 抛出异常 ``` ### 四、典型应用场景 1. DTO与实体模型转换 2. 微服务间数据契约转换 3. 数据库实体到视图模型的映射
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值