在这个非常明显的情况下,C#无法推断出类型参数:
public void Test<T>(IEnumerable<KeyValuePair<string, T>> kvp)
{
Console.WriteLine(kvp.GetType().Name + ": KeyValues");
}
Test(new Newtonsoft.Json.Linq.JObject());
Run Code Online (Sandbox Code Playgroud)
该JObject类型明确实现IEnumerable<KeyValuePair<string, JToken>>,但我收到以下错误:
CS0411: The type arguments for method cannot be inferred from the usage.
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
UPD:对于将此问题标记为重复的编辑:请注意我的方法签名不接受IEnumerable<T>,但是IEnumerable<KeyValuePair<string, T>>.该JObject类型实现IEnumerable两次,但只有一个实现匹配此约束 - 因此不应该有歧义.
UPD:这是一个完整的自包含repro没有JObject:https:
//gist.github.com/impworks/2eee2cd0364815ab8245b81963934642
是否可以ui-router在Angular 1.5应用程序中配置以完全忽略地址栏?
期望的行为是这样的:
更新:我正在创建一个具有非常特定用例的应用程序,而不是一个网站.普通的网页浏览实践不适用于此处.请接受这些要求,即使它似乎与常识相矛盾.
我正在将一个应用程序从 .NET Framework 移植到 .NET Core,它使用运行时代码生成。
这是旧代码:
var refs = new []
{
typeof(Foo).Assembly.Location,
typeof(Enumerable).Assembly.Location
};
var options = new CompilerParameters(refs)
{
GenerateExecutable = false,
GenerateInMemory = true
};
var provider = new CSharpCodeProvider();
var result = provider.CompileAssemblyFromSource(options, rawText);
return result.CompiledAssembly;
Run Code Online (Sandbox Code Playgroud)
使用 Roslyn 的新代码如下所示:
var root = Path.GetDirectoryName(typeof(object).Assembly.Location);
var typeRefs = new[]
{
typeof(object).Assembly.Location,
Path.Combine(root, "System.Collections.dll"),
Path.Combine(root, "System.Runtime.dll"),
typeof(Foo).Assembly.Location,
typeof(Enumerable).Assembly.Location
};
var source = Measure("SourceText", () => SourceText.From(rawText));
var parseOptions = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7);
var syntaxTree = Measure("Parse", () => SyntaxFactory.ParseSyntaxTree(source, parseOptions)); …Run Code Online (Sandbox Code Playgroud) 在Outlook 2010中,您可以创建联系人并将其添加到组.有没有办法获得这些群组及其中的联系人列表?以下是我访问联系人的方式:
var outlook = new Outlook.Application().GetNamespace("MAPI");
var folder = outlook.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
foreach (var curr in folder.Items.OfType<Outlook.ContactItem>())
{
...
}
Run Code Online (Sandbox Code Playgroud)
我不是指默认联系人文件夹,例如"联系人"和"建议的联系人".
我正在使用Reflection.Emit,我想创建一个类似于C#中定义的以下类型的类型:
class A
{
public Tuple<A, int> GetValue(int x)
{
return new Tuple<A, int>(this, x);
}
}
Run Code Online (Sandbox Code Playgroud)
诀窍是我需要使用BCL中的泛型类型,它使用我的自定义类型作为泛型参数.
我正在搞乱以下片段:
var asmName = new AssemblyName("Test");
var access = AssemblyBuilderAccess.Run;
var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, access);
var module = asm.DefineDynamicModule("Test");
var aType = module.DefineType("A");
var tupleType = typeof(Tuple<,>).MakeGenericType(aType, typeof(int));
var attrs = MethodAttributes.Public;
var method = aType.DefineMethod("GetValue", attrs, tupleType, new [] { typeof(int) });
var gen = method.GetILGenerator();
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldarg_1);
// here is the fail:
var ctor = tupleType.GetConstructor(new [] { …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一种自定义语言,允许从最后一个语句推断出函数返回类型.但是,当发现直接或间接递归函数调用时,类型推断系统显然会失败.
func factorial(a:int) ->
if a == 0
1
else
a * factorial(a - 1)
Run Code Online (Sandbox Code Playgroud)
例如,即使参数类型未指定,F#也会这样做:
let rec fact i =
if i = 0 then 1 else i * fact (i-1)
Run Code Online (Sandbox Code Playgroud)
这个系统如何运作?