{"record":{"id":"04db56058bc5acdc","repo":"JeffreySu/WeiXinMPSDK","slug":"getinstance","errorCode":null,"errorMessage":"GetInstance 失败，此类型无法自动生成：","messagePattern":"GetInstance 失败，此类型无法自动生成：","errorType":"exception","errorClass":"TenpayApiRequestException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs","lineNumber":846,"sourceCode":"            }\n        }\n\n        /// <summary>\n        /// 获取实例\n        /// </summary>\n        /// <typeparam name=\"T\"></typeparam>\n        /// <param name=\"throwIfFaild\"></param>\n        /// <returns></returns>\n        private T GetInstance<T>(bool throwIfFaild)\n            where T : ReturnJsonBase\n        {\n            if (typeof(T).IsClass)\n            {\n                return Senparc.CO2NET.Helpers.ReflectionHelper.CreateInstance<T>(typeof(T).FullName, typeof(T).Assembly.GetName().Name);\n            }\n            else if (throwIfFaild)\n            {\n                throw new TenpayApiRequestException(\"GetInstance 失败，此类型无法自动生成：\" + typeof(T).FullName);\n            }\n            return null;\n        }\n    }\n}\n","sourceCodeStart":828,"sourceCodeEnd":852,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs#L828-L852","documentation":"GetInstance<T> tries to auto-create an instance of T: for classes it uses ReflectionHelper.CreateInstance; for non-class types (interfaces, value types without parameterless ctor semantics) with throwIfFaild=true it throws TenpayApiRequestException stating the type cannot be auto-generated. It means the requested response type could not be instantiated via reflection.","triggerScenarios":"Calling GetInstance<T>(throwIfFaild: true) with T being an interface, abstract class, struct, or a class without an accessible parameterless constructor.","commonSituations":"Specifying an interface (e.g. IRequestResult) or abstract base type as the generic response type; passing a DTO whose constructor requires arguments; generic type inference pulling in the wrong type.","solutions":["Use a concrete, publicly constructible class with a parameterless constructor as T (e.g. a concrete response model).","Add a public parameterless constructor to your response class.","Pass throwIfFaild: false if null is acceptable and handle the null return.","If T is an interface, change the call site to the implementing concrete type."],"exampleFix":"// before\nvar result = TenPayApiRequest.GetInstance<IRefundResponse>(throwIfFaild: true);\n// after\nvar result = TenPayApiRequest.GetInstance<RefundResponse>(throwIfFaild: true); // concrete class with parameterless ctor","handlingStrategy":"type-guard","validationCode":"if (typeof(T).IsInterface || typeof(T).IsAbstract || typeof(T).GetConstructor(Type.EmptyTypes) == null)\n    throw new ArgumentException($\"{typeof(T).Name} must be a concrete class with a parameterless constructor.\");","typeGuard":"bool CanAutoCreate<T>() => typeof(T).IsClass && !typeof(T).IsAbstract && typeof(T).GetConstructor(Type.EmptyTypes) != null;","tryCatchPattern":"try { result = TenPayApiRequest.GetInstance<T>(); }\ncatch (TenpayApiRequestException ex) { logger.LogError(ex, \"Cannot auto-create {Type}\", typeof(T).FullName); throw; }","preventionTips":["Always use concrete response model types with public parameterless ctors.","Avoid interfaces/abstract classes as generic response type arguments.","Unit-test generic helpers with the actual T values used in production."],"tags":["reflection","generics","instantiation"],"backgroundTag":"class-not-found","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}