如何延长一个LINQ到SQL类,而不必让每产生code时间的变化?而不必、时间、LINQ、SQL

2023-09-07 10:14:35 作者:人走茶凉

从评论更新:

我需要通过自己的参数来扩展LINQ到SQL类和不想要触碰任何生成的类。任何更好的suggestes的欢迎。但我也不想这样做所有的属性分配的所有时间,如果再在LINQ到SQL类正在发生变化。因此,如果vstudio生成新的属性的一类,我有我自己的扩展属性分开,而新的类本身innerited

I need to extend linq-to-sql classes by own parameters and dont want to touch any generated classes. Any better suggestes are welcome. But I also don't want to do all attributes assignments all time again if the linq-to-sql classes are changing. so if vstudio generates new attribute to a class i have my own extended attributes kept separate, and the new innerited from the class itself

原题:

我不知道这是否是可能的。 我有一类汽车类mycar从类车延。 类mycar也有一个字符串列表。唯一区别。

i'm not sure if it's possible. I have a class car and a class mycar extended from class car. Class mycar has also a string list. Only difference.

我怎么能现在施放任何汽车对象为mycar对象没有指派所有 用手属性每个。像:

How can i cast now any car object to a mycar object without assigning all attributes each by hand. Like:

Car car = new Car();

MyCar mcar = (MyCar) car;

MyCar mcar = new MyCar(car);

或不过,我可以延长汽车用自己的变量,并不需要做总是

or however i can extend car with own variables and don't have to do always

Car car = new Car();
MyCar mcar = new MyCar();
mcar.name = car.name;
mcar.xyz = car.xyz;
...

感谢。

推荐答案

在回答关于这个问题的意见,对LINQ到SQL类生成的部分。这意味着你可以有声明为部分相同的类名独立code文件添加你需要的额外的属性。

In response to your comment on the question, the Linq-To-Sql classes are generated as partial. This means you can have separate code files with the same class names declared as partial to add the extra properties you want.

例如。你的灵到SQL设计器类将是:

E.g. Your Ling-To-Sql designer class will be:

public partial class Car
{
     .... lots of auto generated stuff ....
}

您可以有自己独立的文件(在同一个项目)呼吁Car.cs:

You can have your own separate file (in the same project) called Car.cs:

public partial class Car
{
     public MyCustomStuff{ get; set; }
}

和两班将被合并到一起。只要确保他们在同一个命名空间。

And the two classes will be merged together. Just make sure they are in the same namespace.