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);
}
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
- c# 代码常用 2017(为自己)
- 为自己的C# ORM 写一个代码自动生成器
- C#中为自己的代码自动添加版权信息
- C#知识点(为自己整理)
- C# 常用代码(转转)
- Unity3D自己常用代码
- Unity3D自己常用代码
- 常用的C#代码(每日更新)
- c#常用代码
- 常用C#代码:Network
- 常用C#代码:WinApp
- C# 常用代码
- c#常用代码
- [转]C# 常用代码
- C#常用代码
- C#常用代码
- C#常用代码总结
- C# ArcGlobe常用代码
- 信号处理——信号频域变换
- Java输入输出流
- 安卓scrollview和list view嵌套时的解决
- UICollectionView的Cell加载动画
- jquery与ajax的引用
- c# 代码常用 2017(为自己)
- android网络访问之HttpURLConnection vs OkHttp
- 洛谷P2672 售货员题解 (记忆化搜索,状压DP)
- QT + WIN32 调试信息输出
- Java虚拟机(1) -- JDK和JRE
- 判断闰年
- 炫龙毒刺x6 安装win7+ubuntu16.04双系统
- Android相关属性的介绍:android:exported
- JS获取当前IP地址与地区