You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

泛型类型继承:寻求简化泛型模式应用的技术方案

Alright, let's tackle this problem head-on. Since your framework restricts that specific pattern you referenced, we can use a generic-based approach to reuse those repeated static and instance methods across your classes/structs—here's how to make it work, with examples that align with the StaticObject<T>: T vibe you're aiming for.

Generic Wrapper Class for Instance Method Reuse

First off, C# doesn’t let you inherit from a type parameter directly (so public class StaticObject<T> : T won’t compile), but we can create a generic wrapper that holds an instance of T and exposes all your shared instance operations. This keeps your reuse logic centralized while working within framework constraints.

public class SharedInstanceOps<T> where T : class, new()
{
    private readonly T _targetInstance;

    // Wrap a new instance of T
    public SharedInstanceOps()
    {
        _targetInstance = new T();
    }

    // Wrap an existing instance you already have
    public SharedInstanceOps(T existingInstance)
    {
        _targetInstance = existingInstance ?? throw new ArgumentNullException(nameof(existingInstance));
    }

    // Example shared instance method: Generic validation logic
    public bool Validate()
    {
        // Use a common interface for type-safe logic
        if (_targetInstance is IValidatable validatable)
        {
            return validatable.RunValidation();
        }
        // Fallback or error if the type doesn't support validation
        throw new InvalidOperationException($"Type {typeof(T).Name} doesn't implement IValidatable");
    }

    // Example: Generic data serialization
    public string SerializeToJson()
    {
        return System.Text.Json.JsonSerializer.Serialize(_targetInstance);
    }
}

// Define a common interface to enforce required methods on target types
public interface IValidatable
{
    bool RunValidation();
}

Extension Methods for a "Native" Method Feel

If you want those shared operations to feel like they’re directly part of your original classes/structs (instead of calling through a wrapper), generic extension methods are perfect. They let you "attach" methods to T without inheritance.

public static class SharedTypeExtensions<T> where T : ISharedBehavior
{
    // Instance extension method: Acts like a built-in method on T
    public static bool Validate(this T instance)
    {
        return instance.ValidateCore();
    }

    // Another instance extension: Quick data export
    public static string ExportData(this T instance)
    {
        return System.Text.Json.JsonSerializer.Serialize(instance);
    }

    // For static method reuse: Use a generic static method (C# doesn't support static extensions)
    public static T CreateAndInitialize()
    {
        var newInstance = Activator.CreateInstance<T>();
        newInstance.InitializeDefaults();
        return newInstance;
    }
}

// Interface to enforce required methods on all target types
public interface ISharedBehavior
{
    bool ValidateCore();
    void InitializeDefaults();
}

Static Method Reuse with Generic Classes

Static methods are trickier because they belong to the type, not an instance. For C# 11+, you can use static abstract interfaces to enforce static method contracts on your target types, making the generic logic type-safe. For older versions, reflection works as a fallback.

C# 11+ (Static Abstract Interfaces)

public static class SharedStaticOps<T> where T : IHasStaticLogic
{
    // Shared static validation logic
    public static bool IsTypeValid()
    {
        return T.RunStaticValidation();
    }

    // Shared static data loading
    public static T LoadFromDefaultSource()
    {
        var rawData = T.FetchDefaultData();
        return Activator.CreateInstance<T>(rawData);
    }
}

// Static abstract interface to define static method requirements
public interface IHasStaticLogic
{
    static abstract bool RunStaticValidation();
    static abstract object FetchDefaultData();
}

Older C# Versions (Reflection Fallback)

If you can’t use static abstract interfaces, reflection lets you call static methods on T without explicit contracts:

public static class SharedStaticOps<T>
{
    public static bool IsTypeValid()
    {
        var staticMethod = typeof(T).GetMethod(
            "RunStaticValidation",
            System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static
        );

        if (staticMethod == null)
            throw new InvalidOperationException($"Type {typeof(T).Name} lacks a public static RunStaticValidation method");

        return (bool)staticMethod.Invoke(null, null);
    }
}

Key Wins of This Approach

  • No duplicate code: All shared logic lives in one place, so updates only happen once
  • Type-safe: Generic constraints prevent you from using the logic with incompatible types
  • Flexible: Works for both instance and static methods, adapting to your existing classes/structs
  • Framework-compliant: Bypasses the restricted pattern while delivering the reuse you need

内容的提问来源于stack exchange,提问作者GoldBishop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:19:37