c# 代码常用 2017(为自己)

来源:互联网 发布:淘宝电子商务模式分析 编辑:程序博客网 时间:2024/06/16 01:55
Environment.CurrentDirectory  在Unity里,是到,打开 显示 Assets和ProjectSettings的那层
修改文件夹里的文件名:因为我的lua文件(后缀为.lua)被莫名其妙的改成文本文件,并且后缀都变成.lua.txt
string inputPath = "C:/Users/Administrator/Desktop/SZClient7/Assets";
            if (!Directory.Exists(inputPath)) return ; 
            string[] pathArr = Directory.GetFiles(inputPath, "*.Lua.txt", SearchOption.AllDirectories);


            Console.WriteLine("pathArr : " + pathArr.Length);
            for (int i = 0; i < pathArr.Length; i++)
            {
                Console.WriteLine("pathArr:   " + pathArr[i]);
                //pathArr[i] = pathArr[i].Replace(".lua", "");
                string newPath = System.IO.Path.ChangeExtension(pathArr[i], ".lua");
                File.Move(pathArr[i], newPath);
                Console.WriteLine("bb:   " + newPath);
            }

引用类型 深拷贝:

public static T DeepClone<T>(T obj)
        {
            using (var ms = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(ms, obj);
                ms.Position = 0;

                return (T) formatter.Deserialize(ms);
            }
        }



枚举遍历
public enum EMyType 
    [System.ComponentModel.Description("A类型")] 
    TypeA = 1, 
    [System.ComponentModel.Description("B类型")] 
    TypeB = 1, 
    [System.ComponentModel.Description("C类型")] 
    TypeC = 2, 
}   
接着遍历其枚举名的方法是
[csharp] view plain copy
foreach (var name in Enum.GetNames(typeof(EMyType))) 
    Console.WriteLine(name); 
接着遍历其枚举名与枚举值的方法是
[csharp] view plain copy
foreach (var value in Enum.GetValues(typeof(EMyType))) 
    Console.WriteLine(string.Format("{0}={1}", value.ToString(), Convert.ToInt32(value))); 
接着遍历其枚举名,枚举值与描述属性的字符串的方法是
[csharp] view plain copy
foreach (var value in Enum.GetValues(typeof(EMyType))) 
    object[] objAttrs = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true); 
    if (objAttrs != null &&  
        objAttrs.Length > 0) 
    
        DescriptionAttribute descAttr = objAttrs[0] as DescriptionAttribute; 
        Console.WriteLine(string.Format("[{0}]", descAttr.Description)); 
    
    Console.WriteLine(string.Format("{0}={1}", value.ToString(), Convert.ToInt32(value))); 
}

枚举反射,根据枚举中的val或者data读取值和描述

举例实用:

复制代码
 1             Type t = Assembly.Load("XXX.SDK").GetType("XXX.SDK.Entities." + ObjType); 2             var dataList = new List<object>(); 3             foreach (var value in Enum.GetValues(t)) 4             { 5  6                 object[] objAttrs = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true); 7                 DescriptionAttribute descAttr = objAttrs[0] as DescriptionAttribute; 8                 var date = new 9                 {10                     Description = descAttr.Description,11                     Name = value.ToString(),12                     value = Convert.ToInt32(value)13                 };14                 dataList.Add(date);15             }
复制代码

根据枚举的名字,读出枚举的描述,内容和值。

 

复制代码
         UserCardStatus userCardStatu = (UserCardStatus)Enum.Parse(typeof(UserCardStatus), result.user_card_status, true);           string txt = GetEnumDescription(userCardStatu);        public string GetEnumDescription(Enum enumValue)        {            string str = enumValue.ToString();            FieldInfo field = enumValue.GetType().GetField(str);            object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);            if (objs == null || objs.Length == 0) return str;            DescriptionAttribute da = (DescriptionAttribute)objs[0];            return da.Description;        }
复制代码

根据枚举的单个内容,读出描述

0 0