c# – 使用多个泛型类型参数进行类型推断
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 使用多个泛型类型参数进行类型推断,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3156字,纯文字阅读大概需要5分钟。
内容图文
我不明白为什么C#在以下完整情况下不会推断出类型:
public interface IThing {}
public class Thing1 : IThing {}
public class Thing2 : IThing {}
public interface IContainer {}
public class Container1 : IContainer
{
public IThing A { get { return new Thing1(); } }
public IThing B { get { return new Thing2(); } }
}
public class Container2 : IContainer
{
public IThing C { get { return new Thing1(); } }
public IThing D { get { return new Thing2(); } }
}
public class SomeClass
{
public void PerformTask() {}
}
public static class ExtensionMethods
{
// This function behaves as I would expect, inferring TContainer
public static TContainer DoStuffWithThings<TContainer>(this TContainer container, Func<TContainer, IThing> getSomething, Func<TContainer, IThing> getSomethingElse)
where TContainer : IContainer
{
var something = getSomething.Invoke(container);
var somethingElse = getSomethingElse.Invoke(container);
// something and something else are the things we specify in our lambda expressions with respect to the container
return container;
}
// The method in question
public static TCustomReturnType DoStuffWithThings<TContainer, TCustomReturnType>(this TContainer container, Func<TContainer, IThing> getSomething, Func<TContainer, IThing> getSomethingElse)
where TContainer : IContainer
where TCustomReturnType : new()
{
var something = getSomething.Invoke(container);
var somethingElse = getSomethingElse.Invoke(container);
// Do stuff with the things just as above
// This time we return our custom type
return new TCustomReturnType();
}
}
public class Driver
{
public static void Main(string[] args)
{
var container1 = new Container1();
var container2 = new Container2();
// I can do stuff with the things for each container, returning the container each time.
container1.DoStuffWithThings(c => c.A, c => c.B)
.DoStuffWithThings(c => c.B, c => c.A);
container2.DoStuffWithThings(c => c.C, c => c.D)
.DoStuffWithThings(c => c.D, c => c.C);
// Now we try to do the same but with the custom return type
container1.DoStuffWithThings<Container1, SomeClass>(c => c.A, c => c.B)
.PerformTask();
// As you can see, the compiler requires us to specify Container1 as the container type.
// Why is it not inferred? It is called from an instance of Container1.
// The behavior I expect is for container1.DoStuffWithThings<SomeClass>(...) to infer
// the container type and return a new instance of SomeClass.
}
}
基本思想是,当只有一个泛型类型参数时,编译器会推断出类型.当我添加一秒时,编译器也没有推断(它显然无法推断第二个,但我不确定为什么它不能推断第一个).我的问题是为什么没有推断出容器的类型?
解决方法:
原因是第二个类型参数没有在任何函数参数中使用.所以它的类型无法从参数用法中推断出来.
如果你有这样的方法签名(显然不等同于你的代码,只是一个例子):
public static TResult DoStuffWithThings<TContainer, TResult>(
this TContainer container,
Func<TContainer, TResult> getSomething)
然后它就能从参数中推断出泛型类型.
如果要避免指定第一个参数,可以将方法拆分为两个函数,并在泛型类型中隐藏中间参数,如下所示:
public static IntermediateResult<T> DoStuffWithThings<T>(this T container)
public class IntermediateResult<T>
{
public WithReturnType<TResult>()
}
然后它称为
var result = container.DoStuffWithThings().WithReturnType<Result>();
内容总结
以上是互联网集市为您收集整理的c# – 使用多个泛型类型参数进行类型推断全部内容,希望文章能够帮你解决c# – 使用多个泛型类型参数进行类型推断所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。