{"record":{"id":"12ace293e20ff68b","repo":"JeffreySu/WeiXinMPSDK","slug":"getinstance-tenpayhttpclient","errorCode":null,"errorMessage":"GetInstance 失败，此类型无法自动生成：","messagePattern":"GetInstance 失败，此类型无法自动生成：","errorType":"exception","errorClass":"TenpayApiRequestException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPayHttpClient.cs","lineNumber":314,"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":296,"sourceCodeEnd":320,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPayHttpClient.cs#L296-L320","documentation":"GetInstance<T> tries to auto-create an instance of T via reflection (ReflectionHelper.CreateInstance) when T is a class; if T is not a class (interface, value type, etc.) and throwIfFaild is true, it throws TenpayApiRequestException naming the type's full name. Generic result deserialization calls this when the expected response type can't be instantiated.","triggerScenarios":"Calling generic API helpers with T as an interface or non-instantiable type, or ReflectionHelper.CreateInstance failing to find a parameterless constructor on T while throwIfFaild is true.","commonSituations":"Using an interface (e.g. IOrdersResult) as the generic parameter; a response DTO without a public parameterless constructor; value types passed accidentally as T.","solutions":["Use a concrete class with a public parameterless constructor as T.","Replace interface generic parameters with their concrete implementations.","If you intentionally allow null results, call with throwIfFaild: false instead of letting it throw."],"exampleFix":"// before\nvar result = client.GetInstance<IOrderQueryResult>(true);\n// after\nvar result = client.GetInstance<OrderQueryResult>(true); // concrete class","handlingStrategy":"type-guard","validationCode":"if (typeof(T).IsInterface || !typeof(T).IsClass)\n    throw new ArgumentException(\"T must be a concrete class with a parameterless constructor.\");","typeGuard":"bool IsInstantiable<T>() where T : notnull => typeof(T).IsClass && !typeof(T).IsAbstract && typeof(T).GetConstructor(Type.EmptyTypes) != null;","tryCatchPattern":"try { var result = client.GetInstance<T>(true); }\ncatch (TenpayApiRequestException ex) { logger.LogError(ex, \"Cannot instantiate response type {Type}\", typeof(T).FullName); }","preventionTips":["Always use concrete DTO classes with public parameterless constructors as generic parameters.","Add a reflection-based startup test that instantiates every response type you use."],"tags":["reflection","generics","deserialization","wechat-pay"],"backgroundTag":"invalid-argument-value","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"}