zoukankan      html  css  js  c++  java
  • 反射(Reflection)一、

    (感谢张子阳老师的著作《.Net之美》一书,感谢hcw_peter老师分享的关于DataTable文章)

    1.创建Define类,创建状态枚举

    
    
    public enum BookingStatus
    {
        未提交 = 1,
        已提交,
        已取消,
        受理中,
        已退回,
        已定妥 = 6,
        已过期
    }
     
    public class Defines
    {
        public Defines()
        { }
    }
    
    

    2.在Unity中创建实例,使用反射遍历枚举字段

    using UnityEngine;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System;
    
    using System.Collections.Generic;
    using UnityEditorInternal.VersionControl;
    using System.Reflection;
    
    using UnityEngine.UI;
    using System.IO;
    using System.Xml;
    
    public class SoftwareRun : MonoBehaviour
    {
        public DataTable m_dataTable = new DataTable();
        Text text;
    
        void Start ()
        {
            m_dataTable = GetDataTable("StudyReflection");
            text = GameObject.Find("Text").GetComponent<Text>();
            ConvertToXmlToString(m_dataTable);
        }
    
        void Update ()
        {
        
        }
    
        private  DataTable GetDataTable(string _dataTableName)
        {
            //创建类型
            Type t = typeof(BookingStatus);
    
            //获取字段信息对象集合
            FieldInfo[] fileArray = t.GetFields();
    
            DataTable table = new DataTable(_dataTableName);
    
            //创建列
            table.Columns.Add("Name", Type.GetType("System.String"));
            table.Columns.Add("Value", Type.GetType("System.Int32"));
    
            //遍历集合
            foreach (FieldInfo field in fileArray)
            {
                if (!field.IsSpecialName)
                {
                    DataRow row = table.NewRow();
                    row[0] = field.Name;
                    row[1] = Convert.ToInt32(field.GetRawConstantValue());
                    table.Rows.Add(row);
                }
            }
            return table;
    
        }
    
        private void ConvertToXmlToString(DataTable _dataTable)
        {
            TextWriter tw = new StringWriter();
    
            _dataTable.TableName = _dataTable.TableName.Length == 0 ? "StudyReflection" : _dataTable.TableName;
    
            _dataTable.WriteXmlSchema(tw);
            _dataTable.WriteXml(tw);
            
    
            text.text = tw.ToString();
    
    
        }
    }

    3.最后在Unity中的编译

     

    Dean二十七
  • 相关阅读:
    浅拷贝在项目中的应用
    MVC3中使用AuthorizeAttribute特性来完成登陆权限的验证
    一个面向对象的JS例子,很好的支持了开闭原则(不要重复你昨天的代码)
    c++学习笔记5
    JS跨域访问操作iframe
    Select函数
    MacOS10.8.3+Xcode4.6+IOS6.1 编译FFmpeg,简单使用
    eclipse部署,在tomcat中找不到eclipse发布的项目。eclipse更改项目发布路径
    初识Volley(二)
    MySQL 5.0 迁移到 MariaDB 10.0.2 存储过程无法迁移
  • 原文地址:https://www.cnblogs.com/Dean27/p/6017444.html
Copyright © 2011-2022 走看看