有没有办法在运行时设置类型的 GUID?

发布于 2024-10-16 09:33:52 字数 383 浏览 7 评论 0原文

如何动态设置类型的 Guid?

愚蠢的问题,但我有一个在多个第三方 COM 对象中完全相同的接口,但每个对象都有不同的 GUID。

我有一个看起来像这样的 C# 界面。

[Guid("1F13D3D8-3071-4125-8011-900D2EAC9A7F")]
[InterfaceType(2)]
[TypeLibType(4240)]
public interface UICtrl
{
  //stuff
}

我希望能够在运行时根据用户选择加载的 COM 对象动态更改 GUID。我无法更改元数据,并且 Type.Guid 没有设置属性。有什么想法吗?

我无法使用 Remit.Emit,因为调用程序集不使用它。我真的被困住了!

How can I set a type's Guid dynamically?

Silly question, but I have an interface that is the exact same across several third-party COM Objects, but has a different GUID in each.

I have a C# interface that looks like so.

[Guid("1F13D3D8-3071-4125-8011-900D2EAC9A7F")]
[InterfaceType(2)]
[TypeLibType(4240)]
public interface UICtrl
{
  //stuff
}

I want to be able to change the GUID dynamically at run time depending on which COM object the user chooses to load. I can't change the meta data, and Type.Guid has no set property. Any ideas?

I can't use Remit.Emit because the calling assembly doesn't use it. I'm really stuck!

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

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

发布评论

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

评论(2

过期以后 2024-10-23 09:33:52

所以我最终通过使用@SLaks 的答案和我自己的答案来解决这个问题。基本上,我获取了我的父界面并从中生成了一个具有我想要的 GUID 的子界面。

AssemblyName aName = new AssemblyName("MulticasterAssembly");
            AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
            ModuleBuilder mb = ab.DefineDynamicModule("MulticasterModule");
            TypeBuilder tb = mb.DefineType("MainOCXMultiCaster", TypeAttributes.Public);
            tb.SetParent(typeof(AxUICtrlEventMulticaster));

            ConstructorInfo cInfo = typeof(GuidAttribute).GetConstructor(new Type[] {typeof(string)});
            CustomAttributeBuilder cab = new CustomAttributeBuilder(cInfo, new object[] { mOCXType.GUID.ToString() });
            tb.SetCustomAttribute(cab);

            ConstructorBuilder cb = tb.DefineDefaultConstructor(MethodAttributes.Public);

            Type childEventMulticaster = tb.CreateType();                

            object o = Activator.CreateInstance(childEventMulticaster);
            childEventMulticaster.InvokeMember("host", BindingFlags.SetProperty, null, o, new object[] { this });

So I ended up fixing this by using a part of @SLaks's answer and my own. Basically I took my parent interface and generated a child interface from it which had the GUID I wanted.

AssemblyName aName = new AssemblyName("MulticasterAssembly");
            AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
            ModuleBuilder mb = ab.DefineDynamicModule("MulticasterModule");
            TypeBuilder tb = mb.DefineType("MainOCXMultiCaster", TypeAttributes.Public);
            tb.SetParent(typeof(AxUICtrlEventMulticaster));

            ConstructorInfo cInfo = typeof(GuidAttribute).GetConstructor(new Type[] {typeof(string)});
            CustomAttributeBuilder cab = new CustomAttributeBuilder(cInfo, new object[] { mOCXType.GUID.ToString() });
            tb.SetCustomAttribute(cab);

            ConstructorBuilder cb = tb.DefineDefaultConstructor(MethodAttributes.Public);

            Type childEventMulticaster = tb.CreateType();                

            object o = Activator.CreateInstance(childEventMulticaster);
            childEventMulticaster.InvokeMember("host", BindingFlags.SetProperty, null, o, new object[] { this });
甜嗑 2024-10-23 09:33:52

尝试创建三个空的[Guid]接口,它们继承具有所有成员的基接口。

Try making three empty [Guid]'d interfaces which inherit a base interface with all of the members.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文