JeffreySu/WeiXinMPSDK · error · TenpayApiRequestException

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

Error message

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

What it means

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.

Solutions

  1. Use a concrete, publicly constructible class with a parameterless constructor as T (e.g. a concrete response model).
  2. Add a public parameterless constructor to your response class.
  3. Pass throwIfFaild: false if null is acceptable and handle the null return.
  4. If T is an interface, change the call site to the implementing concrete type.

Example fix

// before
var result = TenPayApiRequest.GetInstance<IRefundResponse>(throwIfFaild: true);
// after
var result = TenPayApiRequest.GetInstance<RefundResponse>(throwIfFaild: true); // concrete class with parameterless ctor
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof(T).IsInterface || typeof(T).IsAbstract || typeof(T).GetConstructor(Type.EmptyTypes) == null)
    throw new ArgumentException($"{typeof(T).Name} must be a concrete class with a parameterless constructor.");

Type guard

bool CanAutoCreate<T>() => typeof(T).IsClass && !typeof(T).IsAbstract && typeof(T).GetConstructor(Type.EmptyTypes) != null;

Try / catch

try { result = TenPayApiRequest.GetInstance<T>(); }
catch (TenpayApiRequestException ex) { logger.LogError(ex, "Cannot auto-create {Type}", typeof(T).FullName); throw; }

Prevention

When it happens

Trigger: Calling GetInstance<T>(throwIfFaild: true) with T being an interface, abstract class, struct, or a class without an accessible parameterless constructor.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs:846

            }
        }

        /// <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)