克隆 Class 定义 (PCL)

Cloning a Class Definition (PCL)

问题: 是否可以使用反射克隆 class 定义?我不是在谈论浅克隆或深度克隆。我说的是定义克隆。我想要一个 class 和一个不在所有实例之间共享的静态变量,而只是我创建的定义。我(或库)稍后需要能够从此 class 创建一个实例。

问题: 你看,我需要这个是因为以下情况,

有一个库希望我为它提供一个具有特定静态方法的类型。但是在我的例子中,这个静态方法需要比较来自另一种类型的非静态字段的两个值。这使得无法将具有信息的实例传递给 class,因为它尚未初始化。查看以下情况示例:

class MasterClass 
{
    public int SomeInfo {get; set;} = 10;
    public void PeresentClass()
    {
        SOMELIBRARY.RegisterType(typeof(StaticClass));
    }
}
class StaticClass
{
    public static bool CanCreate(int someVar)
    {
        // I need to compare someVar with the SomeInfo property of MasterClass instance that presented this type to the SOMELIBRARY.
    }
    public StaticClass()
    {
        // Something irrelevant
    }
}

在上面的示例中,我无法控制 SOMELIBRARY 以及他们决定编写代码的方式。但是他们似乎有些想先调用CanCreate方法,然后在满足要求的情况下创建class的实例。

但是,要使 CanCreate 正常工作,我首先需要访问 class 的实例,将 StaticClass 呈现给 SOMELIBRARY。而且我无法将 MasterClass 设置为静态,因为每次都有不止一个 class 处于活动状态。

我唯一能想到的方法是重新定义一个新的 StaticClass,其中一个静态字段指向定义它的 MasterClass(或克隆定义)。然而,我对反思的了解使我无法做到这一点。所以我在这里问这甚至有可能吗?我真的很想能够在 PCL 个人资料下做到这一点。

真实世界: 只是为了了解更多信息,我实际上是在谈论 XAMARIN.iOSNSUrlProtocol class,特别是 CanInitWithRequest 方法。

可能的解决方案: 经过更多思考,我发现解决这个问题的另一种方法是使StaticClass泛型;这样做允许我根据 type 定义拥有一个静态变量。但是,为了使其工作,我需要能够在 运行 时间创建唯一且可能为空的类型。这可能吗?

XAMARIN.iOS: 不幸的是 Reflection.Emit 在 iOS 上不可用所以现在我不相信这在反正。仍在等待您对情况的评论。

https://developer.xamarin.com/guides/ios/advanced_topics/limitations/#System.Reflection.Emit

有多种方法可以在运行时创建 class,这似乎是您要问的。您的问题似乎已排除 System.Reflection.Emit,因此您可能想探索 some of the other answers on this topic 以查看它们是否适合您的平台 (Xamarin.IOS)。

也就是说,您的问题似乎表明您的实现中存在代码异味。您正在尝试在 API 注册函数中映射一个 class 实例,该函数依赖静态方法来指示资源是否适合处理某种类型的请求 (canInitWithRequest)。这个函数应该只指示注册的 NSURLProtocol class 是否能够处理特定的请求类型,它可能不应该依赖于另一个对象中的某些 class 属性系统.

更好的方法可能是让您的 NSURLProtocol 实例在被底层框架调用时在运行时查找共享资源。例如,类似于以下内容:

static class SystemMap {
    // Store some mapping information in a commonly accessible system resource
    // In this case a simple static class that wraps up a dictionary
    static Dictionary<Type, Master> systemMap = new Dictionary<Type, Master>();

    // Allow registered components to be accessed
    public static Master getRegisteredMaster(Type handlerType) {
        return systemMap[handlerType];
    }

    // Allow new registrations to be made in your system
    public static void registerNewMaster(Master registrant, Type handlerType) {
        systemMap[handlerType] = registrant;
    }
}

class Master {
    // This would be your custom class that you instantiate throughout your system
    public string name;
    public int someVar { get; set; } = new Random().Next(1, 100);
    public Master(string name) {
        this.name = name;
    }
}

class BaseHandlerType {
    // This would be NSURLProtocol
}

class Handler1 : BaseHandlerType {
    // This would be canInitWithRequest
    public static bool CanCreate(int someVar) {
        Master myMaster = SystemMap.getRegisteredMaster(typeof(Handler1));
        return someVar > myMaster.someVar;
    }
}

class Handler2 : BaseHandlerType {
    //... Register various handler types to various "Master" instances in your system
    // This is a concrete implementation of NSURLProtocol
}

class Handler3 : BaseHandlerType {
    //... Register various handler types to various "Master" instances in your system
    // This is a concrete implementation of NSURLProtocol
}

class SystemFactory {
    // Use a factory method to instantiate the system components and plug things together
    public void initializeSystem() {
        var masterA = new Master("a");
        var masterB = new Master("b");
        var masterC = new Master("c");
        SystemMap.registerNewMaster(masterA, typeof(Handler1));
        SystemMap.registerNewMaster(masterB, typeof(Handler2));
        SystemMap.registerNewMaster(masterC, typeof(Handler3));
        SomeLibrary.register(typeof(Handler1));
        SomeLibrary.register(typeof(Handler2));
        SomeLibrary.register(typeof(Handler3));
    }
}

static class SomeLibrary {
    public static void register(Type handlerType) {
        // This represents the API registration
    }
}

此模式可能会帮助您建立您试图通过 class 在运行时创建来实现的组件之间的关系。此模式将允许您的各种不同类型的处理程序(即 NSURLProtocol classes)在调用时访问不同的 Master 实例。在此示例中,masterA 映射到 Handler1masterB 映射到 Handler2,依此类推。

经过大量搜索,我发现在 Xamarin.iOS 下甚至无法在 C# 中创建空类型,因此,我不得不更改我的代码以符合 Apple 的期望 API.

在我的例子中,我最终保留了一个 MasterClass 的所有实例的列表以在 StaticClass 中使用;构造函数和静态方法都遍历列表并将所需的 MasterClass 与请求匹配。这里有一个风险,这样做最终会导致内存泄漏,因为永远不会收集 MasterClass 的实例,但在我的情况下,这不是问题。