C#样式静态类-托管包装 [英] C# Style Static Class - Managed Wrapper

查看:82
本文介绍了C#样式静态类-托管包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何做到的?

我需要一个C#样式的静态类,但是,我发现的所有示例都不解释本机对象的构造\破坏.

我能想到的最好的就是这样.

How do you do this?

I need a C# style static class, but, none of the examples I''ve found account for construction\destruction of the native object.

The best I can figure, is something like.

public ref class Managed
{
    private:
        static Native* m_native;
    public:
      
        static void CallNativeMethod()
        {
            if (!m_native)
                m_native = new m_native();

            m_native->DoSomething();
             
            if (m_native)
                delete m_native;

            m_native = NULL;
        }
};



我不明白,我知道这是可以做到的,而且看起来完全像C#样式的静态类,我已经看过了,但是缺少源代码. (Google也不是我的朋友.)

有人知道该怎么做更好吗?因为那太讨厌了. (我有很多方法需要实现,因为每个方法看起来都很糟糕,而且很烂.)

(我在这里输入了这个信息,我什至不知道该文件是否可以编译,因此,不必介意.)



I don''t get it, I know this can be done, and be made to look exactly like a C# style static class, I''ve seen it, but, lack source code. (Google is not being my friend either.)

Anybody know how to do this, better? Because that''s just nasty. (I have a lot of methods I need to implement, doing that for each one would look bad, and just suck.)

(I typed this up here, I don''t even know if that would compile, so, don''t mind that.)

推荐答案

我最终弄清楚了我不得不猜测出如何正确执行此操作,因为没有人会提供解决方案,而搜索没有任何用处.

因此,对于所有搜索了该答案但找不到的人来说,它是:

只是直接从托管代码调用本机函数,似乎不应该工作,但是,我知道,您发现的每个示例都向您展示了如何包装本机类,但是那样做就不会尝试创建C#样式的静态类时可以正常工作.

I ended up figuring out how to do this properly, I had to guess, since no one would offer the solution, and searching yielded nothing useful.

So, for all the people who have searched for this answer, and couldn''t find it, here it is:

Just call your native functions directly from the managed code, doesn''t seem like it should work, but, it does, I know, EVERY example you find shows you how to wrap native classes, but, going that way won''t work when you''re trying to create a C# style static class.

#pragma once
#pragma unmanaged
#include "Native.h"
#pragma managed

#using <mscorlib.dll>
using namespace System;

public ref class Managed sealed
{
    public:
      static void ManagedCall()
      {
          NativeFunction();
      }
};



(我认为,只要将本机代码包装在托管结构中,任何本机代码都可以工作.因此,您可以使用本机extern变量等来创建静态属性.)



(I think any native code works, as long as it''s wrapped in a managed construct. So, you could make static properties, using native extern variables, etc,.)


这篇关于C#样式静态类-托管包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆