C# 泛型类型函数,使泛型方法失败 System.String
C# Generic Type Function, Make Generic Method Fails with System.String
我正在使用 .net Core 5.0.302,但在使用反射调用泛型方法时遇到问题
函数是
public static T Parse<T>(List<Token> tokenList, int startPos) where T : new(){
if(tokenList[startPos].tokenData.ToString() == "["){
// ParseArray(tokenList);
throw new NotImplementedException();
}
else if(tokenList[startPos].tokenData.ToString() == "{"){
return ParseObject<T>(tokenList, startPos);
}else{
T retVal = new T();
retVal = (T)tokenList[startPos].tokenData;
return retVal;
}
}
试图调用此方法的代码在这里 -
MethodInfo method = typeof(JsonParser).GetMethod(nameof(JsonParser.Parse), BindingFlags.Static | BindingFlags.Public);
method = method.MakeGenericMethod(typeof(string));
object parsedType = method.Invoke(null, new object[]{tokenList, i + 3});
props[z].SetValue(typeInst, parsedType);
这是我收到的错误消息
System.Security.VerificationException : Method ParseLib.JsonParser.Parse: type argument 'System.String' violates the constraint of type parameter 'T'.
Stack Trace:
at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
at ParseLib.JsonParser.ParseObject[T](List1 tokenList, Int32 startPos) in /home/user/Projects/MyProjects/JsonParseSharp/ParseLib/JsonParser.cs:line 54 at ParseLib.JsonParser.Parse[T](List
1 tokenList, Int32 startPos) in /home/user/Projects/MyProjects/JsonParseSharp/ParseLib/JsonParser.cs:line 15
at ParseTests.JsonParserTest.TestParseBasicObject() in /home/user/Projects/MyProjects/JsonParseSharp/ParseTests/JsonParserTest.cs:line 16
----- Inner Stack Trace -----
at System.RuntimeMethodHandle.GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation)
at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
无法弄清楚为什么 System.String 违反了类型参数 'T' 如果我将 typeof(string) 替换为 typeof(int),一切都会按预期进行。有谁知道这里可能是什么问题?
提前致谢!
where T : new()
您的 Parse 方法有一个类型约束,表明类型 T 必须有一个 public 无参数构造函数。
在c#中,字符串构造函数至少需要一个参数来构造一个字符串。
var str = new string("abc")
但是可以使用无参数构造函数实例化 int:
var num = new int()
我正在使用 .net Core 5.0.302,但在使用反射调用泛型方法时遇到问题
函数是
public static T Parse<T>(List<Token> tokenList, int startPos) where T : new(){
if(tokenList[startPos].tokenData.ToString() == "["){
// ParseArray(tokenList);
throw new NotImplementedException();
}
else if(tokenList[startPos].tokenData.ToString() == "{"){
return ParseObject<T>(tokenList, startPos);
}else{
T retVal = new T();
retVal = (T)tokenList[startPos].tokenData;
return retVal;
}
}
试图调用此方法的代码在这里 -
MethodInfo method = typeof(JsonParser).GetMethod(nameof(JsonParser.Parse), BindingFlags.Static | BindingFlags.Public);
method = method.MakeGenericMethod(typeof(string));
object parsedType = method.Invoke(null, new object[]{tokenList, i + 3});
props[z].SetValue(typeInst, parsedType);
这是我收到的错误消息
System.Security.VerificationException : Method ParseLib.JsonParser.Parse: type argument 'System.String' violates the constraint of type parameter 'T'. Stack Trace: at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e) at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation) at ParseLib.JsonParser.ParseObject[T](List
1 tokenList, Int32 startPos) in /home/user/Projects/MyProjects/JsonParseSharp/ParseLib/JsonParser.cs:line 54 at ParseLib.JsonParser.Parse[T](List
1 tokenList, Int32 startPos) in /home/user/Projects/MyProjects/JsonParseSharp/ParseLib/JsonParser.cs:line 15 at ParseTests.JsonParserTest.TestParseBasicObject() in /home/user/Projects/MyProjects/JsonParseSharp/ParseTests/JsonParserTest.cs:line 16 ----- Inner Stack Trace ----- at System.RuntimeMethodHandle.GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation) at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
无法弄清楚为什么 System.String 违反了类型参数 'T' 如果我将 typeof(string) 替换为 typeof(int),一切都会按预期进行。有谁知道这里可能是什么问题?
提前致谢!
where T : new()
您的 Parse 方法有一个类型约束,表明类型 T 必须有一个 public 无参数构造函数。
在c#中,字符串构造函数至少需要一个参数来构造一个字符串。
var str = new string("abc")
但是可以使用无参数构造函数实例化 int:
var num = new int()