[英]collection of derived class
I'm refactoring my code of separation of concern (SoC). 我正在重构关注点分离(SoC)的代码。 Move non-AutoCAD code away to separate dll, so Common, Data Access, web service, AutoCAD related, … in different dll files.
将非AutoCAD代码移到单独的dll,以便将Common,Data Access,Web服务,与AutoCAD相关的…放在不同的dll文件中。
Here is sample of my class: 这是我班的例子:
// non-AutoCAD dll
public class Loc
{
public int Id;
public string Name;
...
}
// AutoCAD dll
public class AcadLoc : Loc
{
public ObjectId oid;
}
// non-AutoCAD dll
public class Locs : List<Loc>
{
public Loc Get_By_Id(long id)
{
return this.SingleOrDefault(n => n.Id == id);
}
}
// AutoCAD dll
public class AcadLocs : Locs // or List<AcadLoc> ?
{
}
My question is, can I run something like 我的问题是,我可以运行类似
AcadLocs myAcadLocs = new AcadLocs();
AcadLoc myAcadLoc = myAcadLocs.Get_By_Id(1); // How to make this works?
I tried: 我试过了:
public class AcadLocs : Locs // or List<AcadLoc> ?
{
public AcadLoc Get_By_Id(int id)
{
return this.SingleOrDefault(n => n.Id == id);
}
}
it would not works. 这是行不通的。
Now I change to this: 现在,我更改为:
public interface ILocData<T> where T : new() {}
// non-AutoCAD dll
public class Loc : ILocData<Loc>
{
public int Id;
public string Name;
...
}
// AutoCAD dll
public class AcadLoc : Loc, ILocData<AcadLoc>
{
public ObjectId oid;
}
public class LocDataCollection<T, TCollection> : List<T>
where T : ILocData<T>, new()
where TCollection : LocDataCollection<T, TCollection>, new()
{
}
// non-AutoCAD dll
public class Locs : LocDataCollection<Loc, Locs>
{
public Loc Get_By_Id(int id)
{
return this.SingleOrDefault(n => n.Id == id);
}
public bool Check()
{
foreach (Loc loc in this)
{
....
}
}
}
// AutoCAD dll
public class AcadLocs : LocDataCollection<AcadLoc, AcadLocs>
{
public AcadLoc Get_By_Id(int id)
{
return this.SingleOrDefault(n => n.Id == id);
}
}
Now, 现在,
AcadLoc myAcadLoc = myAcadLocs.Get_By_Id(1); // works
but 但
myAcadLocs.Check(); // failed
Thanks 谢谢
Wes 韦斯
I think what you need is extension method 我认为您需要的是扩展方法
public static class MyExtension
{
public static Loc Get_By_Id(this Locs l,int id)
{
return l.SingleOrDefault(n => n.Id == id);
}
public static bool Check(this Locs l)
{
foreach (Loc loc in l)
{
....
}
}
}
Once you have this code in place then just call this methods like this 一旦有了此代码,就可以像这样调用此方法
myAcadLocs.Get_By_Id(1);
myAcadLocs.Check();
myLocs.Get_By_Id(1);
myLocs.Check();
I refer to your first attempt. 我指的是您的第一次尝试。
public class AcadLocs : Locs // or List<AcadLoc> ?
If you want to derive from Locs
or List<AcadLoc>
depends on how you want to use this class. 是否要从
Locs
或List<AcadLoc>
派生取决于您要如何使用此类。 If you want to declare common members or if it is necessary that you can assign an instance of AcadLocs
to a variable of type Locs
like this 如果你想声明成员共同或者如果可以的实例分配有必要
AcadLocs
到类型的变量Locs
这样
Locs locs = new AcadLocs();
then you surely need to derive from Locs
. 那么您肯定需要从
Locs
派生。
For this method I would use the OfType<>
method like this: 对于此方法,我将使用
OfType<>
方法,如下所示:
public class AcadLocs : Locs // or List<AcadLoc> ?
{
public AcadLoc Get_By_Id(int id)
{
return this.OfType<AcadLoc>().SingleOrDefault(n => n.Id == id);
}
}
OfType<AcadLoc>
creates an IEnumeralbe<AcadLoc>
out of the existing sequence selecting only elements that are of the specified type ( AcadLoc
). OfType<AcadLoc>
IEnumeralbe<AcadLoc>
从现有序列中创建一个IEnumeralbe<AcadLoc>
,仅选择指定类型( AcadLoc
)的元素。 This will help you with your Check
method, too. 这也将帮助您使用
Check
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.