[英]Generic T to call overloaded functions
I want to do something like this: 我想做这样的事情:
public class Object1 {
public int number{get;set;}
}
public class Object2 {
public int number{get;set;}
}
public class Object3 {
public int number{get;set;}
}
main();
public void main(){
var objects1 = new List<Object1>{new Object1{number=1} , new Object1{number=2}};
test<Object1>(objects1);
}
public List<Object3> test<T>(IEnumerable<T> objs){
var rv = new List<Object3>();
foreach (var o in objs)
{
var foo = overloaded(o);
rv.Add(foo);
}
return rv;
}
public Object3 overloaded(Object1 obj){
// Run very specific things to Object1
return new Object3{number=obj.number+1};
}
public Object3 overloaded(Object2 obj){
// Run very specific things to Object2
return new Object3{number=obj.number+2};
}
You can directly run/edit the code here, with error handling: http://csharppad.com/gist/6ff5f13cac8f0e5735be 您可以在此处直接运行/编辑代码,并进行错误处理: http : //csharppad.com/gist/6ff5f13cac8f0e5735be
The error I get is Argument 1: cannot convert from 'T' to 'Object1'
- So how can I do this? 我得到的错误是
Argument 1: cannot convert from 'T' to 'Object1'
- 那我怎么能这样做? The idea is that Object1 and Object2 have 95% of their code identical, it's that last 5% that I need to have it do something specific for each. 我们的想法是Object1和Object2有95%的代码相同,这是我需要让它为每个代码执行特定操作的最后5%。
您可以在test
方法中使用dynamic
,只需注意性能影响:
overloaded((dynamic)obj);
After playing around I was able to come up with this 在玩完之后我就能想到这个
public class Object1 {
public int number{get;set;}
}
public class Object2 {
public int number{get;set;}
}
public class Object3 {
public int number{get;set;}
}
main();
public void main(){
var objects1 = new List<Object1>{new Object1{number=1} , new Object1{number=2}};
test<Object1>(objects1);
}
public List<Object3> test<T>(IEnumerable<T> objs){
var rv = new List<Object3>();
foreach (var o in objs)
{
if(typeof(T) == typeof(Object1)){
rv.Add(overloaded((Object1)(object)o));
} else {
rv.Add(overloaded((Object2)(object)o));
}
}
return rv;
}
public Object3 overloaded(Object1 obj){
return new Object3{number=obj.number+1};
}
public Object3 overloaded(Object2 obj){
return new Object3{number=obj.number+2};
}
This works, but seems hacky to me. 这有效,但对我来说似乎很烦人。 Wondering what the best way is!
想知道最好的方法是什么!
I would reverse your thinking. 我会改变你的想法。
Try this: 尝试这个:
private void test<T>(T obj){
// Do common stuff
}
public void overloaded(Object1 obj){
test(obj);
Console.WriteLine("Do Object 1 stuff");
}
public void overloaded(Object2 obj){
test(obj);
Console.WriteLine("Do Object 2 stuff");
}
and call overloaded
instead of calling test
. 并调用
overloaded
而不是调用test
。
Dependency Injection 依赖注入
interface IObject
{
int number {get;set;}
}
public class Object1 : IObject {
public int number{get;set;}
}
public class Object2 : IObject {
public int number{get;set;}
}
public class Object3 : IObject {
public int number{get;set;}
}
public IObject overloaded(IObject obj){
// Run very specific things to Object1
return new IObject {number=obj.number+1};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.