简单C#类转非托管C++

开发工具: C#2013
功能:
针对简单的C#类,生成对应的非托管C++类,并生成托管C++的转换函数。
应用场景:
界面层、数据层C#,逻辑层C++。
简单的C#类:类型只包括 double string List

源码下载:

简单的C#类生成对应的C#类-C#文档类资源-CSDN文库icon-default.png?t=LA46https://download.csdn.net/download/he_zhidan/37948430

        public class CFieldInfo
        {
            public CFieldInfo(string strFullTypeName, string strName, string strGenericTypeArguments)
            {
                m_strFullTypeName = strFullTypeName;
                m_strName = strName;
                m_strGenericTypeArguments = strGenericTypeArguments;
            }
            public readonly string m_strFullTypeName;
            public readonly string m_strName;
            public readonly string m_strGenericTypeArguments;
        }

        public class CClassInfo
        {
            public string m_strName;//类名
            public List fielss = new List(); 
        }

        class CCollectClass
        {
            public static List Create(string strSpaceName)
            {
                return Create(Assembly.GetExecutingAssembly(), strSpaceName);
            }
            public static List Create(Assembly assem, string strSpaceName)
            {
                Type[] types = assem.GetTypes();
                List classInfos = new List();
                for (int i = 0; i < types.Length; i++)
                {
                    if( strSpaceName ==  types[i].Namespace )
                    {
                        classInfos.Add(CreateClass(types[i]));
                    }                   
                }
                return classInfos;
            }

            static CClassInfo CreateClass(Type type)
            {
                CClassInfo classInfo = new CClassInfo();
                classInfo.m_strName = type.Name;               
                
                var Fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                foreach( var field in Fields )
                {
                    if (field.FieldType.GenericTypeArguments.Length > 0)
                    {
                        classInfo.fielss.Add(new CFieldInfo(field.FieldType.Name, field.Name, field.FieldType.GenericTypeArguments[0].Name));
                    }
                    else
                    {
                        classInfo.fielss.Add(new CFieldInfo(field.FieldType.Name, field.Name,""));
                    }
                }
                return classInfo;
            }
        }

        class CChangRule
        {
            public CChangRule(string strSharpType, string strChangeFun, string strCPlusType, string strSharpGenericTypeArguments, string strCPlusGenericTypeArguments, string strChangeToSharpFun)
            {
                 m_strCSharpType = strSharpType;
                 m_strChangeToCLRFun = strChangeFun;
                m_strCPlusType = strCPlusType;
                m_strSharpGenericTypeArguments = strSharpGenericTypeArguments;
                m_strCPlusGenericTypeArguments = strCPlusGenericTypeArguments;
                m_strChangeToCPlusFun = strChangeToSharpFun;
            }
            public string GetCPlusName()
            {
                if ("" == m_strCPlusGenericTypeArguments)
                {
                    return m_strCPlusType;
                }
                return m_strCPlusType + "<" + m_strCPlusGenericTypeArguments + ">";

            }
            public readonly string m_strCSharpType;
            public readonly string m_strChangeToCLRFun;
            public readonly string m_strCPlusType;
            public readonly string m_strSharpGenericTypeArguments;
            public readonly string m_strCPlusGenericTypeArguments;
            public readonly string m_strChangeToCPlusFun;
        }

        class CChangeRules
        {
            public void AddRule(string strSharpType, string strChangeFun, string strCPlusType, string strSharpGenericTypeArguments, string strCPlusGenericTypeArguments, string strChangeToCPlusFun="")
            {
                CChangRule rule = new CChangRule(strSharpType, strChangeFun, strCPlusType, strSharpGenericTypeArguments, strCPlusGenericTypeArguments, strChangeToCPlusFun);
               m_rules.Add(rule);
            }
            public CChangRule GetRule(CFieldInfo field)
            {
                for( int i = 0 ; i < m_rules.Count ; i++ )
                {
                    if (m_rules[i].m_strCSharpType != field.m_strFullTypeName)
                    {
                        continue;
                    }
                    if ("" != field.m_strGenericTypeArguments)
                    {
                        if (field.m_strGenericTypeArguments != m_rules[i].m_strSharpGenericTypeArguments)
                        {
                            continue;
                        }
                    }
                    return m_rules[i];
                }
                return new CChangRule(field.m_strFullTypeName, "=", field.m_strFullTypeName, "", "","");
            }
            protected List m_rules = new List();
        }
        class CCreateDataClass
        {
            public  string Create(CClassInfo classInfo,CChangeRules changRules)
            {
                string str = "class " + m_strExportDefine + " C" + classInfo.m_strName + "\n" + "{\npublic:\n" + ChangeFields(classInfo,changRules) + "};";
                return str;
            }

             string ChangeFields(CClassInfo classInfo, CChangeRules changRules)
            {
                string str = "";
                for( int i = 0 ; i < classInfo.fielss.Count ; i++ )
                {
                    var f = classInfo.fielss[i];
                    CChangRule rule = changRules.GetRule(f);
                    str += rule.GetCPlusName() + " " + f.m_strName + ";\r\n";
                }
                return str;
            }
            public string m_strExportDefine = "CPLUSALOG_API_20211029";
        }

        class CCreateChangeClass
        {
            public  void Create(CClassInfo classInfo, CChangeRules changRules)
            {
                if( "" == m_strChangeClassName )
                {
                    m_strChangeClassName = "CChange" + classInfo.m_strName;
                }
                m_strDeclare = "class " + m_strExportDefine + " " + m_strChangeClassName + "\n" + "{\npublic:\n"
                    + CreateChangeToCLRDeclare(classInfo, changRules) + CreateChangeToCPlusDeclare(classInfo, changRules) + "};\n";
                m_strDefine = CreateChangeToCLR(classInfo, changRules) + "\n" +  CreateChangeToCPlus(classInfo, changRules);
            }

            string CreateChangeToCLRDeclare(CClassInfo classInfo, CChangeRules changRules)
            {
                return "static " + classInfo.m_strName + "^ To(const C" + classInfo.m_strName + "& src);\n";               
            }

            string CreateChangeToCPlusDeclare(CClassInfo classInfo, CChangeRules changRules)
            {
                return "static C" + classInfo.m_strName + " To(" + classInfo.m_strName + "^ src);\n";                
            }


             string CreateChangeToCLR(CClassInfo classInfo, CChangeRules changRules)
            {
                string str = classInfo.m_strName + "^ " + m_strChangeClassName + "::To(const C" + classInfo.m_strName + "& src)\n{\n";
                str += classInfo.m_strName + "^ ret = gcnew " + classInfo.m_strName + "();\n";
                for (int i = 0; i < classInfo.fielss.Count; i++)
                {
                    CChangRule rule = changRules.GetRule(classInfo.fielss[i]);
                    str += "ret->" + classInfo.fielss[i].m_strName + " = " + rule.m_strChangeToCLRFun +"(src." + classInfo.fielss[i].m_strName + ");\n";
                }
                str += "return ret;\n";
                return str + "}\n";
            }

             string CreateChangeToCPlus(CClassInfo classInfo, CChangeRules changRules)
            {
                string str = "C" + classInfo.m_strName + " " + m_strChangeClassName + "::To(" + classInfo.m_strName + "^ src)\n{\n";
                str += "C" +  classInfo.m_strName + " ret;\n";
                for (int i = 0; i < classInfo.fielss.Count; i++)
                {
                    CChangRule rule = changRules.GetRule(classInfo.fielss[i]);
                    str += "ret." + classInfo.fielss[i].m_strName + " = " + rule.m_strChangeToCPlusFun +"(src->" + classInfo.fielss[i].m_strName + ");\n";
                }
                str += "return ret;\n";
                return str + "}\n";
            }

            public string m_strExportDefine = "";//"CPLUSALOG_API_20211029";
            public string m_strChangeClassName = "";
            public string m_strDeclare = "";
            public string m_strDefine = "";
        }

你可能感兴趣的:(闻缺陷则喜,c#,c++,开发语言)