如何使用使用接口的扩展方法
因此,我正在尝试将具有这样的身体进行静态类,称为Action Extensions,
public static class ActionExtensions {
public static IContainer Add<TObjectType>(this IContainer container, TObjectType obj) {
return container;
}
}
并使用Interface Icontainer。 我还有一个名为“学生”的课程:
public class Student, IContainer {
public IList<int> Grades{ get;set;}
}
现在我想做类似的事情:
var student = new Student();
student.Add(3); //adding grade 3 to list of student's grades
但是我不想为学生班定义一个添加方法。宁愿使用我的方法中的方法。 这甚至可能吗?
So i'm trying to make a static class called ActionExtensions which has a body like this:
public static class ActionExtensions {
public static IContainer Add<TObjectType>(this IContainer container, TObjectType obj) {
return container;
}
}
and uses interface IContainer.
I also have a class called Student with list of grades:
public class Student, IContainer {
public IList<int> Grades{ get;set;}
}
Now I want to do something like this:
var student = new Student();
student.Add(3); //adding grade 3 to list of student's grades
But i don't want to define an Add method to a Student class. Rather use my method from ActionExtensions.
Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能。您可以这样做:
您可以像这样使用它
It is possible. You could do it like this:
You can than use it like this