Castle DynamicProxy 生成的类名

发布于 2024-10-02 13:39:04 字数 202 浏览 7 评论 0原文

有谁知道是否可以控制通过 Castle DynamicProxy 生成的类型的名称?我希望利用保留 Castle 生成的程序集的能力,向我的项目添加一些具有某些特定功能的附加类,但我希望能够控制这些生成的代理类型的名称。任何帮助将不胜感激。

实际上,我计划保留这些类的实例以及作为 NHibernate 代理来源的原始类的实例。因此,我需要这些名称在程序集的多代中保持一致。

Does anybody know if it is possible to control the names of the types generated through Castle DynamicProxy? I was hoping to take advantage of the ability to persist the assembly generated by Castle to add some additional classes with some specific functionality to my project, but I would like to be able to control the names of these generated proxy types. Any help would be greatly appreciated.

I actually plan to persist instances of these classes as well as instances of the original classes that are the sources of the proxies with NHibernate. So, I need these names to be consistent across multiple generations of the assembly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以可爱出名 2024-10-09 13:39:04

我做了一些有趣的挖掘。使用 INamingScope 似乎可以指定代理名称,但插入 INamingScope 远非那么简单。您需要创建自己的 ProxyFactoryFactory,这将创建一个与 NHibernate.ByteCode.Castle.ProxyFactory 相同的 ProxyFactory,除了它会初始化 ProxyGenerator:

public class CustomProxyFactory : AbstractProxyFactory {
    private static readonly ProxyGenerator ProxyGenerator = new ProxyGenerator(new CustomProxyBuilder());
    // remainder of code is identical
}

public class CustomProxyBuilder : DefaultProxyBuilder {
    public CustomProxyBuilder() : base(new CustomModuleScope()) {}
}

public class CustomModuleScope : ModuleScope {
    public CustomModuleScope() : base(false, false, new CustomNamingScope(), DEFAULT_ASSEMBLY_NAME, DEFAULT_FILE_NAME, DEFAULT_ASSEMBLY_NAME, DEFAULT_FILE_NAME) {}
}

public class CustomNamingScope : INamingScope {
    public CustomNamingScope() {}

    private CustomNamingScope(INamingScope parent) {
        ParentScope = parent;
    }

    public string GetUniqueName(string suggestedName) {
        // your naming logic goes here
    }

    public INamingScope SafeSubScope() {
        return new CustomModuleScope(this);
    }

    public INamingScope ParentScope { get; private set; }
}

老实说,我还没有尝试过运行或编译其中任何一个。只是深入研究 NHibernate 和 Castle.Core 源代码。希望它能给你一些想法......

I did some interesting digging. Specifying proxy names appears to be possible using an INamingScope, but it is far from straightforward to get the INamingScope wedged in. You would need to create your own ProxyFactoryFactory, which would create a ProxyFactory identical to NHibernate.ByteCode.Castle.ProxyFactory, except it would initilize ProxyGenerator:

public class CustomProxyFactory : AbstractProxyFactory {
    private static readonly ProxyGenerator ProxyGenerator = new ProxyGenerator(new CustomProxyBuilder());
    // remainder of code is identical
}

public class CustomProxyBuilder : DefaultProxyBuilder {
    public CustomProxyBuilder() : base(new CustomModuleScope()) {}
}

public class CustomModuleScope : ModuleScope {
    public CustomModuleScope() : base(false, false, new CustomNamingScope(), DEFAULT_ASSEMBLY_NAME, DEFAULT_FILE_NAME, DEFAULT_ASSEMBLY_NAME, DEFAULT_FILE_NAME) {}
}

public class CustomNamingScope : INamingScope {
    public CustomNamingScope() {}

    private CustomNamingScope(INamingScope parent) {
        ParentScope = parent;
    }

    public string GetUniqueName(string suggestedName) {
        // your naming logic goes here
    }

    public INamingScope SafeSubScope() {
        return new CustomModuleScope(this);
    }

    public INamingScope ParentScope { get; private set; }
}

I honestly haven't tried running or compiling any of this. Just digging through the NHibernate and Castle.Core source code. Hopefully it gives you some ideas...

笑着哭最痛 2024-10-09 13:39:04

看一下 NHContrib 中的 ProxyGenerators 项目。它允许您预先生成 NHibernate 的延迟加载代理。

http://nhforge.org/wikis/proxygenerators10/default.aspx

是否使用无论是否有 ProxyGenerator,您都可以通过 Proxy Factory 将自定义代理集成到 NHibernate 中。在 hibernate.cfg.xml 中:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="proxyfactory.factory_class">YOUR_PROXY_FACTORY_FACTORY</property>
  </session-factory>
</hibernate-configuration>

Take a look at the ProxyGenerators project in NHContrib. It allows you to pre-generate NHibernate's lazy loading proxies.

http://nhforge.org/wikis/proxygenerators10/default.aspx

Whether you use the ProxyGenerators or not, you integrate your custom proxies into NHibernate via the Proxy Factory Factory. In hibernate.cfg.xml:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="proxyfactory.factory_class">YOUR_PROXY_FACTORY_FACTORY</property>
  </session-factory>
</hibernate-configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文