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二十七
  • 相关阅读:
    js,timeout,promise执行顺序
    vue数据响应的坑
    css中的block与none
    javascript 私有化属性,和公共属性
    animal与@keyframe
    css3中的translate,transform,transition的区别
    AngularJS实现原理
    bootstrap添加多个模态对话框支持
    ajax
    jQuery点击弹出层,弹出模态框,点击模态框消失
  • 原文地址:https://www.cnblogs.com/Dean27/p/6017444.html
Copyright © 2011-2022 走看看