使用 GetElementType() 时获取空引用
Getting a null reference while using GetElementType()
我正在尝试使我的 graphQL 突变变得通用。但是在运行时我得到一个错误。
错误提示:未将对象引用设置为对象的实例。
public class ApplicationMutation<T> : ObjectGraphType where T: BaseModel
{
public ApplicationMutation()
{
this.Name = "RootMutation";
var nameofT = typeof(T).GetElementType().Name;
this.Field<AddPayloadType<T>>(
"add" + nameofT,
arguments: new InputQueryArguments<AddInputType<T>>(),
resolve: context =>
{
var input = context.GetArgument<AddInput<T>>("input");
var result = Activator.CreateInstance<T>();
{
Name = "1337 p40c355I73m";
};
return new AddPayload<T>(input, result);
});
this.Field<UpdatePayloadType<T>>(
"update" + nameofT,
arguments: new InputQueryArguments<UpdateInputType<T>>(),
resolve: context =>
{
var input = context.GetArgument<UpdateInput<T>>("input");
var result = Activator.CreateInstance<T>();
{
Name = "rul0r item";
};
return new UpdatePayload<T>(input, result);
});
this.Field<DeletePayloadType<T>>(
"delete" + nameofT,
arguments: new InputQueryArguments<DeleteInputType<T>>(),
resolve: context =>
{
var input = context.GetArgument<DeleteInput<T>>("input");
var result = true;
return new DeletePayload<T>(input, result);
});
}
}
从以下行抛出异常:
var nameofT = typeof(T).GetElementType().Name;
如果需要更多信息,请提出要求。
您只能将 GetElementType()
用于数组、指针和引用类型..
null if the current Type is not an array or a pointer, or is not passed by reference, or represents a generic type or a type parameter in the definition of a generic type or generic method
https://msdn.microsoft.com/en-us/library/system.type.getelementtype.aspx
它将 return null
用于仿制药
我正在尝试使我的 graphQL 突变变得通用。但是在运行时我得到一个错误。
错误提示:未将对象引用设置为对象的实例。
public class ApplicationMutation<T> : ObjectGraphType where T: BaseModel
{
public ApplicationMutation()
{
this.Name = "RootMutation";
var nameofT = typeof(T).GetElementType().Name;
this.Field<AddPayloadType<T>>(
"add" + nameofT,
arguments: new InputQueryArguments<AddInputType<T>>(),
resolve: context =>
{
var input = context.GetArgument<AddInput<T>>("input");
var result = Activator.CreateInstance<T>();
{
Name = "1337 p40c355I73m";
};
return new AddPayload<T>(input, result);
});
this.Field<UpdatePayloadType<T>>(
"update" + nameofT,
arguments: new InputQueryArguments<UpdateInputType<T>>(),
resolve: context =>
{
var input = context.GetArgument<UpdateInput<T>>("input");
var result = Activator.CreateInstance<T>();
{
Name = "rul0r item";
};
return new UpdatePayload<T>(input, result);
});
this.Field<DeletePayloadType<T>>(
"delete" + nameofT,
arguments: new InputQueryArguments<DeleteInputType<T>>(),
resolve: context =>
{
var input = context.GetArgument<DeleteInput<T>>("input");
var result = true;
return new DeletePayload<T>(input, result);
});
}
}
从以下行抛出异常:
var nameofT = typeof(T).GetElementType().Name;
如果需要更多信息,请提出要求。
您只能将 GetElementType()
用于数组、指针和引用类型..
null if the current Type is not an array or a pointer, or is not passed by reference, or represents a generic type or a type parameter in the definition of a generic type or generic method https://msdn.microsoft.com/en-us/library/system.type.getelementtype.aspx
它将 return null
用于仿制药