开发者

T4 copy enum between projects

开发者 https://www.devze.com 2023-02-04 19:18 出处:网络
I have two projects in my solution. I need to copy the values from enum1 in Project1 to enum2 in project2. Is this possible using T4 ?

I have two projects in my solution. I need to copy the values from enum1 in Project1 to enum2 in project2. Is this possible using T4 ?

EDIT

Let me describe my situation a bit better. The project1 where enum1 is, is generated by a tool. There're mostly entities generated from database and an enum of table names. This project1 is used on the server side to interact with the database. The server side does communicate with the client side via WCF. In matter 开发者_JAVA技巧of separation I don't want to share my project1 thru WCF. But there is this one enum with entity names. I use the entity names on the client side to make some task generic. For example I sent some values over WCF (Client -> Server) and I use the entity names to map the values to the appropriate entities.


First of all, yes, you most certainly can build a T4 template that reads your enums in one project, and adds them to another.

However, you probably don't want to do that.

But, if you do, there's an even easier way, you just add the original enum file as a link into the second project. By adding a link, you're not actually making a copy of the file, you're just linking back to it in the other project, and then there's no need to muck around with T4. This, however, assumes you don't want to change the enum in the process in any way.

To add a link, just click Add existing item, as usualy, navigate to the original enum file, but on the Add button, there's a small dropdown menu, which contains the link menu item. Use this, and you'll see that your new file has a small link symbol overlaid on its icon in the Solution Explorer, indicating that it is a link.

However, the reason I said you probably don't want to do that is that even if you copy the entire enum file as it is from one project to the other, the two enums are not the same type.

In other words, if you in one project need to call code in the other project, using the enum from project A when calling code in project B that is declared to use the enum in project B won't work, they're different types. The assembly is part of the identity of the type.

If you need to do this, you either need to declare the enum in project A (or B), and reference that project in the other one, or declare it in a third project, and reference that third project in both A and B.


Maybe you can create the template to read the file from one project and writes in the project that contains the tt. Don't forget to change the namespace of your type if you are going to use both projects in the same point.

The tt:

<#@ template hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>

<#             
var path=@"..\Domain.Model\MyEnum.cs";
path = this.Host.ResolvePath(path);

var content = File.ReadAllText(path);

// mental note: learn regex once for all¡¡¡
content=content.Replace("namespace Domain.Model", "namespace Domain.Dtos"); #>

<#=content#>
0

精彩评论

暂无评论...
验证码 换一张
取 消