JeffreySu/WeiXinMPSDK · error · TenpayApiRequestException

GetInstance 失败,此类型无法自动生成:

Error message

GetInstance 失败,此类型无法自动生成:

What it means

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.

Solutions

  1. Use a concrete class with a public parameterless constructor as T.
  2. Replace interface generic parameters with their concrete implementations.
  3. If you intentionally allow null results, call with throwIfFaild: false instead of letting it throw.

Example fix

// before
var result = client.GetInstance<IOrderQueryResult>(true);
// after
var result = client.GetInstance<OrderQueryResult>(true); // concrete class
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof(T).IsInterface || !typeof(T).IsClass)
    throw new ArgumentException("T must be a concrete class with a parameterless constructor.");

Type guard

bool IsInstantiable<T>() where T : notnull => typeof(T).IsClass && !typeof(T).IsAbstract && typeof(T).GetConstructor(Type.EmptyTypes) != null;

Try / catch

try { var result = client.GetInstance<T>(true); }
catch (TenpayApiRequestException ex) { logger.LogError(ex, "Cannot instantiate response type {Type}", typeof(T).FullName); }

Prevention

When it happens

Trigger: 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.

Common situations: Using an interface (e.g. IOrdersResult) as the generic parameter; a response DTO without a public parameterless constructor; value types passed accidentally as T.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/12ace293e20ff68b. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPayHttpClient.cs:314

            }
        }

        /// <summary>
        /// 获取实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="throwIfFaild"></param>
        /// <returns></returns>
        private T GetInstance<T>(bool throwIfFaild)
            where T : ReturnJsonBase
        {
            if (typeof(T).IsClass)
            {
                return Senparc.CO2NET.Helpers.ReflectionHelper.CreateInstance<T>(typeof(T).FullName, typeof(T).Assembly.GetName().Name);
            }
            else if (throwIfFaild)
            {
                throw new TenpayApiRequestException("GetInstance 失败,此类型无法自动生成:" + typeof(T).FullName);
            }
            return null;
        }
    }
}

View on GitHub (pinned to be573f6f94)