框架上传
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
|
||||
using System;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("01.FluentAPI.CSharp", "System.Object", 0)]
|
||||
[APIDescriptionCN("针对 System.Object 提供的链式扩展,理论上任何对象都可以使用")]
|
||||
[APIDescriptionEN("The chain extension provided by System.object can theoretically be used by any Object")]
|
||||
#endif
|
||||
public static class SystemObjectExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.1
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("将自己传到 Action 委托中")]
|
||||
[APIDescriptionEN("apply self to the Action delegate")]
|
||||
[APIExampleCode(@"
|
||||
new GameObject()
|
||||
.Self(gameObj=>gameObj.name = ""Enemy"")
|
||||
.Self(gameObj=>{
|
||||
Debug.Log(gameObj.name);
|
||||
});"
|
||||
)]
|
||||
#endif
|
||||
public static T Self<T>(this T self, Action<T> onDo)
|
||||
{
|
||||
onDo?.Invoke(self);
|
||||
return self;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.1.1
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("将自己传到 Func<T,T> 委托中,然后返回自己")]
|
||||
[APIDescriptionEN("apply self to the Func<T,T> delegate")]
|
||||
[APIExampleCode(@"
|
||||
new GameObject()
|
||||
.Self(gameObj=>gameObj.name = ""Enemy"")
|
||||
.Self(gameObj=>{
|
||||
Debug.Log(gameObj.name);
|
||||
});"
|
||||
)]
|
||||
#endif
|
||||
public static T Self<T>(this T self, Func<T,T> onDo)
|
||||
{
|
||||
return onDo.Invoke(self);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.2
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("判断是否为空")]
|
||||
[APIDescriptionEN("Check Is Null,return true or false")]
|
||||
[APIExampleCode(@"
|
||||
var simpleObject = new object();
|
||||
|
||||
if (simpleObject.IsNull()) // simpleObject == null
|
||||
{
|
||||
// do sth
|
||||
}")]
|
||||
#endif
|
||||
public static bool IsNull<T>(this T selfObj) where T : class
|
||||
{
|
||||
return null == selfObj;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.3
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("判断不是为空")]
|
||||
[APIDescriptionEN("Check Is Not Null,return true or false")]
|
||||
[APIExampleCode(@"
|
||||
var simpleObject = new object();
|
||||
|
||||
if (simpleObject.IsNotNull()) // simpleObject != null
|
||||
{
|
||||
// do sth
|
||||
}")]
|
||||
#endif
|
||||
public static bool IsNotNull<T>(this T selfObj) where T : class
|
||||
{
|
||||
return null != selfObj;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.36
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("转型")]
|
||||
[APIDescriptionEN("cast")]
|
||||
[APIExampleCode(@"
|
||||
int a = 10;
|
||||
Debug.Log(a.As<float>())
|
||||
// 10
|
||||
")]
|
||||
#endif
|
||||
public static T As<T>(this object selfObj) where T : class
|
||||
{
|
||||
return selfObj as T;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8ef786a6b2b044b69fcc6abb9e695619
|
||||
timeCreated: 1647422404
|
||||
@@ -0,0 +1,275 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("01.FluentAPI.CSharp", "System.String", 1)]
|
||||
[APIDescriptionCN("针对 System.String 提供的链式扩展,理论上任何集合都可以使用")]
|
||||
[APIDescriptionEN("The chain extension provided by System.Collections can theoretically be used by any collection")]
|
||||
#endif
|
||||
public static class SystemStringExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.18
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("检测是否为空或 Empty")]
|
||||
[APIDescriptionEN("Check Whether string is null or empty")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(string.Empty.IsNullOrEmpty());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool IsNullOrEmpty(this string selfStr)
|
||||
{
|
||||
return string.IsNullOrEmpty(selfStr);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.19
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("检测是否为非空且非Empty")]
|
||||
[APIDescriptionEN("Checks both not null and non-empty")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(""Hello"".IsNotNullAndEmpty());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool IsNotNullAndEmpty(this string selfStr)
|
||||
{
|
||||
return !string.IsNullOrEmpty(selfStr);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.20
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("去掉两端空格后,检测是否为空或 Empty")]
|
||||
[APIDescriptionEN("Check if it is Empty or Empty after removing whitespace from both sides")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log("" "".IsTrimNullOrEmpty());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool IsTrimNullOrEmpty(this string selfStr)
|
||||
{
|
||||
return selfStr == null || string.IsNullOrEmpty(selfStr.Trim());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Whether string trim is null or empty
|
||||
/// </summary>
|
||||
/// <param name="selfStr"></param>
|
||||
/// <returns></returns>
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.21
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("去掉两端空格后,检测是否为非 null 且非 Empty")]
|
||||
[APIDescriptionEN("After removing whitespace from both sides, check whether it is non-null and non-empty")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log("" 123 "".IsTrimNotNullAndEmpty());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool IsTrimNotNullAndEmpty(this string selfStr)
|
||||
{
|
||||
return selfStr != null && !string.IsNullOrEmpty(selfStr.Trim());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 缓存
|
||||
/// </summary>
|
||||
private static readonly char[] mCachedSplitCharArray = { '.' };
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.22
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("字符串分割")]
|
||||
[APIDescriptionEN("String splitting")]
|
||||
[APIExampleCode(@"
|
||||
""1.2.3.4.5"".Split('.').ForEach(str=>Debug.Log(str));
|
||||
// 1 2 3 4 5
|
||||
")]
|
||||
#endif
|
||||
public static string[] Split(this string selfStr, char splitSymbol)
|
||||
{
|
||||
mCachedSplitCharArray[0] = splitSymbol;
|
||||
return selfStr.Split(mCachedSplitCharArray);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.23
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("格式化字符串填充参数")]
|
||||
[APIDescriptionEN("The format string populates the parameters")]
|
||||
[APIExampleCode(@"
|
||||
|
||||
var newStr = ""{0},{1}"".FillFormat(1,2);
|
||||
Debug.Log(newStr);
|
||||
// 1,2
|
||||
")]
|
||||
#endif
|
||||
public static string FillFormat(this string selfStr, params object[] args)
|
||||
{
|
||||
return string.Format(selfStr, args);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.24
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("返回包含此字符串的 StringBuilder")]
|
||||
[APIDescriptionEN("Returns a StringBuilder containing this string")]
|
||||
[APIExampleCode(@"
|
||||
var builder = ""Hello"".Builder();
|
||||
builder.Append("" QF"");
|
||||
Debug.Log(builder.ToString());
|
||||
// Hello QF
|
||||
")]
|
||||
#endif
|
||||
public static StringBuilder Builder(this string selfStr)
|
||||
{
|
||||
return new StringBuilder(selfStr);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.25
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("StringBuilder 添加前缀")]
|
||||
[APIDescriptionEN("StringBuilder insert prefix string")]
|
||||
[APIExampleCode(@"
|
||||
var builder = ""I'm liangxie"".Builder().AddPrefix(""Hi!"") ;
|
||||
Debug.Log(builder.ToString());
|
||||
// Hi!I'm liangxie
|
||||
")]
|
||||
#endif
|
||||
public static StringBuilder AddPrefix(this StringBuilder self, string prefixString)
|
||||
{
|
||||
self.Insert(0, prefixString);
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.26
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("字符串解析成 Int")]
|
||||
[APIDescriptionEN("parse string to int")]
|
||||
[APIExampleCode(@"
|
||||
var number = ""123456"".ToInt();
|
||||
Debug.Log(number);
|
||||
// 123456
|
||||
// notice unsafe
|
||||
// 不安全
|
||||
")]
|
||||
#endif
|
||||
public static int ToInt(this string selfStr, int defaulValue = 0)
|
||||
{
|
||||
var retValue = defaulValue;
|
||||
return int.TryParse(selfStr, out retValue) ? retValue : defaulValue;
|
||||
}
|
||||
|
||||
public static long ToLong(this string self, long defaultValue = 0)
|
||||
{
|
||||
var retValue = defaultValue;
|
||||
return long.TryParse(self, out retValue) ? retValue : defaultValue;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.27
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("字符串解析成 Int")]
|
||||
[APIDescriptionEN("parse string to int")]
|
||||
[APIExampleCode(@"
|
||||
DateTime.Now.ToString().ToDataTime();
|
||||
")]
|
||||
#endif
|
||||
public static DateTime ToDateTime(this string selfStr, DateTime defaultValue = default(DateTime))
|
||||
{
|
||||
return DateTime.TryParse(selfStr, out var retValue) ? retValue : defaultValue;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.28
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("字符串解析成 float")]
|
||||
[APIDescriptionEN("parse string to float")]
|
||||
[APIExampleCode(@"
|
||||
var number = ""123456f"".ToInt();
|
||||
Debug.Log(number);
|
||||
// 123456
|
||||
// notice unsafe
|
||||
// 不安全
|
||||
")]
|
||||
#endif
|
||||
public static float ToFloat(this string selfStr, float defaultValue = 0)
|
||||
{
|
||||
return float.TryParse(selfStr, out var retValue) ? retValue : defaultValue;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.29
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("是否存在中文字符")]
|
||||
[APIDescriptionEN("check string contains chinese or not")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(""你好"".HasChinese());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool HasChinese(this string input)
|
||||
{
|
||||
return Regex.IsMatch(input, @"[\u4e00-\u9fa5]");
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.30
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("是否存在空格")]
|
||||
[APIDescriptionEN("check string contains space or not")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(""你好 "".HasSpace());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool HasSpace(this string input)
|
||||
{
|
||||
return input.Contains(" ");
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.31
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("remove string")]
|
||||
[APIDescriptionEN("check string contains space or not")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(""Hello World "".RemoveString(""Hello"","" ""));
|
||||
// World
|
||||
")]
|
||||
#endif
|
||||
public static string RemoveString(this string str, params string[] targets)
|
||||
{
|
||||
return targets.Aggregate(str, (current, t) => current.Replace(t, string.Empty));
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.39
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("join string")]
|
||||
[APIDescriptionEN("join string")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(new List<string>() { ""1"",""2"",""3""}.StringJoin("",""));
|
||||
// 1,2,3
|
||||
")]
|
||||
#endif
|
||||
public static string StringJoin(this IEnumerable<string> self, string separator)
|
||||
{
|
||||
return string.Join(separator, self);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 16f9643181cd400085f8615855718a05
|
||||
timeCreated: 1647445231
|
||||
@@ -0,0 +1,183 @@
|
||||
|
||||
using System.IO;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("01.FluentAPI.CSharp", "System.IO", 2)]
|
||||
[APIDescriptionCN("针对 System.IO 提供的链式扩展,主要是文件和文件夹的一些 IO 操作")]
|
||||
[APIDescriptionEN("IO chain extension for system. IO, mainly file and folder IO operations")]
|
||||
#endif
|
||||
public static class SystemIOExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.10
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("创建文件夹,如果存在则不创建")]
|
||||
[APIDescriptionEN("Create folder or not if it exists")]
|
||||
[APIExampleCode(@"
|
||||
var testDir = ""Assets/TestFolder"";
|
||||
testDir.CreateDirIfNotExists();"
|
||||
)]
|
||||
#endif
|
||||
public static string CreateDirIfNotExists(this string dirFullPath)
|
||||
{
|
||||
if (!Directory.Exists(dirFullPath))
|
||||
{
|
||||
Directory.CreateDirectory(dirFullPath);
|
||||
}
|
||||
|
||||
return dirFullPath;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.11
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("删除文件夹,如果存在")]
|
||||
[APIDescriptionEN("Delete the folder if it exists")]
|
||||
[APIExampleCode(@"
|
||||
var testDir =""Assets/TestFolder"";
|
||||
testDir.DeleteDirIfExists();
|
||||
")]
|
||||
#endif
|
||||
public static void DeleteDirIfExists(this string dirFullPath)
|
||||
{
|
||||
if (Directory.Exists(dirFullPath))
|
||||
{
|
||||
Directory.Delete(dirFullPath, true);
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.12
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("清空 Dir(保留目录),如果存在")]
|
||||
[APIDescriptionEN("Clear Dir (reserved directory), if exists")]
|
||||
[APIExampleCode(@"
|
||||
var testDir = ""Assets/TestFolder"";
|
||||
testDir.EmptyDirIfExists();
|
||||
")]
|
||||
#endif
|
||||
public static void EmptyDirIfExists(this string dirFullPath)
|
||||
{
|
||||
if (Directory.Exists(dirFullPath))
|
||||
{
|
||||
Directory.Delete(dirFullPath, true);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(dirFullPath);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.13
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("删除文件 如果存在")]
|
||||
[APIDescriptionEN("Delete the file if it exists")]
|
||||
[APIExampleCode(@"
|
||||
var filePath = ""Assets/Test.txt"";
|
||||
File.Create(""Assets/Test"");
|
||||
filePath.DeleteFileIfExists();
|
||||
")]
|
||||
#endif
|
||||
public static bool DeleteFileIfExists(this string fileFullPath)
|
||||
{
|
||||
if (File.Exists(fileFullPath))
|
||||
{
|
||||
File.Delete(fileFullPath);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.14
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("合并路径")]
|
||||
[APIDescriptionEN("Combine path")]
|
||||
[APIExampleCode(@"
|
||||
var path = Application.dataPath.CombinePath(""Resources"");
|
||||
Debug.Log(Path)
|
||||
// projectPath/Assets/Resources
|
||||
")]
|
||||
#endif
|
||||
public static string CombinePath(this string selfPath, string toCombinePath)
|
||||
{
|
||||
return Path.Combine(selfPath, toCombinePath);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.15
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("根据路径获取文件名")]
|
||||
[APIDescriptionEN("get file name by path")]
|
||||
[APIExampleCode(@"
|
||||
var fileName =""/abc/def/b.txt"".GetFileName();
|
||||
Debug.Log(fileName0);
|
||||
// b.txt
|
||||
")]
|
||||
#endif
|
||||
public static string GetFileName(this string filePath)
|
||||
{
|
||||
return Path.GetFileName(filePath);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.16
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("根据路径获取文件名,不包含文件扩展名")]
|
||||
[APIDescriptionEN("Get the file name based on the path, excluding the file name extension")]
|
||||
[APIExampleCode(@"
|
||||
var fileName =""/abc/def/b.txt"".GetFileNameWithoutExtend();
|
||||
Debug.Log(fileName0);
|
||||
// b
|
||||
")]
|
||||
#endif
|
||||
|
||||
public static string GetFileNameWithoutExtend(this string filePath)
|
||||
{
|
||||
return Path.GetFileNameWithoutExtension(filePath);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.17
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("根据路径获取文件扩展名")]
|
||||
[APIDescriptionEN("Get the file extension based on the path")]
|
||||
[APIExampleCode(@"
|
||||
var fileName =""/abc/def/b.txt"".GetFileExtendName();
|
||||
Debug.Log(fileName0);
|
||||
// .txt
|
||||
")]
|
||||
#endif
|
||||
public static string GetFileExtendName(this string filePath)
|
||||
{
|
||||
return Path.GetExtension(filePath);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.156
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("获取文件夹路径")]
|
||||
[APIDescriptionEN("get filePath's folder path")]
|
||||
[APIExampleCode(@"
|
||||
var folderPath =""/abc/def/b.txt"".GetFolderPath();
|
||||
Debug.Log(fileName0);
|
||||
// /abs/def
|
||||
")]
|
||||
#endif
|
||||
public static string GetFolderPath(this string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return Path.GetDirectoryName(path);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 214fbbb4fd6f426f8e9867263025a775
|
||||
timeCreated: 1647438572
|
||||
@@ -0,0 +1,188 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("01.FluentAPI.CSharp", "System.Collections", 3)]
|
||||
[APIDescriptionCN("针对 System.Collections 提供的链式扩展,理论上任何集合都可以使用")]
|
||||
[APIDescriptionEN("The chain extension provided by System.Collections can theoretically be used by any collection")]
|
||||
#endif
|
||||
public static class CollectionsExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.4
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("遍历 IEnumerable")]
|
||||
[APIDescriptionEN("ForEach for IEnumerable")]
|
||||
[APIExampleCode(@"
|
||||
IEnumerable<int> testIEnumerable = new List<int> { 1, 2, 3 };
|
||||
testIEnumerable.ForEach(number => Debug.Log(number));
|
||||
// output
|
||||
// 1
|
||||
// 2
|
||||
// 3
|
||||
new Dictionary<string, string>()
|
||||
{
|
||||
{""name"",""liangxie""},
|
||||
{""company"",""liangxiegame"" }
|
||||
}
|
||||
.ForEach(keyValue => Debug.LogFormat(""key:{0},value:{1}"", keyValue.Key, keyValue.Value));
|
||||
// key:name,value:liangxie
|
||||
// key:company,value:liangxiegame")]
|
||||
#endif
|
||||
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> self, Action<T> action)
|
||||
{
|
||||
foreach (var item in self)
|
||||
{
|
||||
action(item);
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.5
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("List 倒序遍历")]
|
||||
[APIDescriptionEN("Reverse ForEach for List")]
|
||||
[APIExampleCode(@"
|
||||
var testList = new List<int> { 1, 2, 3 };
|
||||
testList.ForEachReverse(number => number.LogInfo());
|
||||
// 3 2 1
|
||||
")]
|
||||
#endif
|
||||
public static List<T> ForEachReverse<T>(this List<T> selfList, Action<T> action)
|
||||
{
|
||||
for (var i = selfList.Count - 1; i >= 0; --i)
|
||||
action(selfList[i]);
|
||||
|
||||
return selfList;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.6
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("遍历 List (可获得索引)")]
|
||||
[APIDescriptionEN("foreach List (can get index)")]
|
||||
[APIExampleCode(@"
|
||||
var testList = new List<string> {""a"", ""b"", ""c"" };
|
||||
testList.Foreach((c,index)=>Debug.Log(index));
|
||||
// 1, 2, 3,
|
||||
")]
|
||||
#endif
|
||||
public static void ForEach<T>(this List<T> list, Action<int, T> action)
|
||||
{
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
action(i, list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.7
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("遍历字典")]
|
||||
[APIDescriptionEN("ForEach Dictionary")]
|
||||
[APIExampleCode(@"
|
||||
var infos = new Dictionary<string,string> {{""name"",""liangxie""},{""age"",""18""}};
|
||||
infos.ForEach((key,value)=> Debug.LogFormat(""{0}:{1}"",key,value);
|
||||
// name:liangxie
|
||||
// age:18
|
||||
")]
|
||||
#endif
|
||||
public static void ForEach<K, V>(this Dictionary<K, V> dict, Action<K, V> action)
|
||||
{
|
||||
var dictE = dict.GetEnumerator();
|
||||
|
||||
while (dictE.MoveNext())
|
||||
{
|
||||
var current = dictE.Current;
|
||||
action(current.Key, current.Value);
|
||||
}
|
||||
|
||||
dictE.Dispose();
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.8
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("合并字典")]
|
||||
[APIDescriptionEN("Merge Dictionaries")]
|
||||
[APIExampleCode(@"
|
||||
var dictionary1 = new Dictionary<string, string> { { ""1"", ""2"" } };
|
||||
var dictionary2 = new Dictionary<string, string> { { ""3"", ""4"" } };
|
||||
var dictionary3 = dictionary1.Merge(dictionary2);
|
||||
dictionary3.ForEach(pair => Debug.LogFormat(""{0}:{1}"", pair.Key, pair.Value));
|
||||
// 1:2
|
||||
// 3:4
|
||||
|
||||
// notice: duplicate keys are not supported.
|
||||
// 注意:不支持重复的 key。
|
||||
")]
|
||||
#endif
|
||||
public static Dictionary<TKey, TValue> Merge<TKey, TValue>(this Dictionary<TKey, TValue> dictionary,
|
||||
params Dictionary<TKey, TValue>[] dictionaries)
|
||||
{
|
||||
return dictionaries.Aggregate(dictionary,
|
||||
(current, dict) => current.Union(dict).ToDictionary(kv => kv.Key, kv => kv.Value));
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.9
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("字典添加新的字典")]
|
||||
[APIDescriptionEN("Dictionary Adds a new dictionary")]
|
||||
[APIExampleCode(@"
|
||||
var dictionary1 = new Dictionary<string, string> { { ""1"", ""2"" } };
|
||||
var dictionary2 = new Dictionary<string, string> { { ""1"", ""4"" } };
|
||||
var dictionary3 = dictionary1.AddRange(dictionary2,true); // true means override
|
||||
dictionary3.ForEach(pair => Debug.LogFormat(""{0}:{1}"", pair.Key, pair.Value));
|
||||
// 1:2
|
||||
// 3:4
|
||||
|
||||
// notice: duplicate keys are supported.
|
||||
// 注意:支持重复的 key。
|
||||
")]
|
||||
#endif
|
||||
public static void AddRange<K, V>(this Dictionary<K, V> dict, Dictionary<K, V> addInDict,
|
||||
bool isOverride = false)
|
||||
{
|
||||
var enumerator = addInDict.GetEnumerator();
|
||||
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
var current = enumerator.Current;
|
||||
if (dict.ContainsKey(current.Key))
|
||||
{
|
||||
if (isOverride)
|
||||
dict[current.Key] = current.Value;
|
||||
continue;
|
||||
}
|
||||
|
||||
dict.Add(current.Key, current.Value);
|
||||
}
|
||||
|
||||
enumerator.Dispose();
|
||||
}
|
||||
|
||||
|
||||
// TODO:
|
||||
public static bool IsNullOrEmpty<T>(this T[] collection) => collection == null || collection.Length == 0;
|
||||
// TODO:
|
||||
public static bool IsNullOrEmpty<T>(this IList<T> collection) => collection == null || collection.Count == 0;
|
||||
// TODO:
|
||||
public static bool IsNullOrEmpty<T>(this IEnumerable<T> collection) => collection == null || !collection.Any();
|
||||
// TODO:
|
||||
public static bool IsNotNullAndEmpty<T>(this T[] collection) => !IsNullOrEmpty(collection);
|
||||
// TODO:
|
||||
public static bool IsNotNullAndEmpty<T>(this IList<T> collection) => !IsNullOrEmpty(collection);
|
||||
// TODO:
|
||||
public static bool IsNotNullAndEmpty<T>(this IEnumerable<T> collection) => !IsNullOrEmpty(collection);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b1be94df70e04fbf880352766fa84326
|
||||
timeCreated: 1647436071
|
||||
@@ -0,0 +1,300 @@
|
||||
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("01.FluentAPI.CSharp", "System.Reflection", 4)]
|
||||
[APIDescriptionCN("针对 System.Reflection 提供的链式扩展")]
|
||||
[APIDescriptionEN("Chain extension provided for System.Reflection")]
|
||||
#endif
|
||||
public static class SystemReflectionExtension
|
||||
{
|
||||
// [UnityEditor.MenuItem("QF/Test")]
|
||||
// public static void Test()
|
||||
// {
|
||||
// "/abc/e.txt".GetFileExtendName().LogInfo();
|
||||
// }
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("通过 Type 创建 Instance")]
|
||||
[APIDescriptionEN("Create Instance By Type")]
|
||||
[APIExampleCode(@"
|
||||
|
||||
interface IA
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class A
|
||||
{
|
||||
}
|
||||
|
||||
IA a = typeof(A).CreateInstance<IA>();
|
||||
|
||||
")]
|
||||
#endif
|
||||
public static T CreateInstance<T>(this Type self) where T : class
|
||||
{
|
||||
// 获取构造函数
|
||||
var constructorInfos = self.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
|
||||
|
||||
// 获取无参构造函数
|
||||
var ctor = Array.Find(constructorInfos, c => c.GetParameters().Length == 0);
|
||||
|
||||
return ctor.Invoke(null) as T;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.32
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("通过反射的方式调用私有方法")]
|
||||
[APIDescriptionEN("call private method by reflection")]
|
||||
[APIExampleCode(@"
|
||||
class A
|
||||
{
|
||||
private void Say() { Debug.Log(""I'm A!"") }
|
||||
}
|
||||
|
||||
new A().ReflectionCallPrivateMethod(""Say"");
|
||||
// I'm A!
|
||||
")]
|
||||
#endif
|
||||
public static object ReflectionCallPrivateMethod<T>(this T self, string methodName, params object[] args)
|
||||
{
|
||||
var type = typeof(T);
|
||||
var methodInfo = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
|
||||
return methodInfo?.Invoke(self, args);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.33
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("通过反射的方式调用私有方法,有返回值")]
|
||||
[APIDescriptionEN("call private method by reflection,return the result")]
|
||||
[APIExampleCode(@"
|
||||
class A
|
||||
{
|
||||
private bool Add(int a,int b) { return a + b; }
|
||||
}
|
||||
|
||||
Debug.Log(new A().ReflectionCallPrivateMethod(""Add"",1,2));
|
||||
// 3
|
||||
")]
|
||||
#endif
|
||||
public static TReturnType ReflectionCallPrivateMethod<T, TReturnType>(this T self, string methodName,
|
||||
params object[] args)
|
||||
{
|
||||
return (TReturnType)self.ReflectionCallPrivateMethod(methodName, args);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.34
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
[APIExampleCode(@"
|
||||
[DisplayName(""A Class"")
|
||||
class A
|
||||
{
|
||||
[DisplayName(""A Number"")
|
||||
public int Number;
|
||||
|
||||
[DisplayName(""Is Complete?"")
|
||||
private bool Complete => Number > 100;
|
||||
|
||||
[DisplayName(""Say complete result?"")
|
||||
public void SayComplete()
|
||||
{
|
||||
Debug.Log(Complete);
|
||||
}
|
||||
}
|
||||
|
||||
var aType = typeof(A);
|
||||
//
|
||||
Debug.Log(aType.HasAttribute(typeof(DisplayNameAttribute));
|
||||
// true
|
||||
Debug.Log(aType.HasAttribute<DisplayNameAttribute>());
|
||||
// true
|
||||
|
||||
// also support MethodInfo、PropertyInfo、FieldInfo
|
||||
// 同时 也支持 MethodInfo、PropertyInfo、FieldInfo
|
||||
")]
|
||||
#endif
|
||||
public static bool HasAttribute<T>(this Type type, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return type.GetCustomAttributes(typeof(T), inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute(this Type type, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return type.GetCustomAttributes(attributeType, inherit).Any();
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute<T>(this PropertyInfo prop, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return prop.GetCustomAttributes(typeof(T), inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute(this PropertyInfo prop, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return prop.GetCustomAttributes(attributeType, inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute<T>(this FieldInfo field, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return field.GetCustomAttributes(typeof(T), inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute(this FieldInfo field, Type attributeType, bool inherit)
|
||||
{
|
||||
return field.GetCustomAttributes(attributeType, inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute<T>(this MethodInfo method, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return method.GetCustomAttributes(typeof(T), inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute(this MethodInfo method, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return method.GetCustomAttributes(attributeType, inherit).Any();
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.35
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
[APIExampleCode(@"
|
||||
[DisplayName(""A Class"")
|
||||
class A
|
||||
{
|
||||
[DisplayName(""A Number"")
|
||||
public int Number;
|
||||
|
||||
[DisplayName(""Is Complete?"")
|
||||
private bool Complete => Number > 100;
|
||||
|
||||
[DisplayName(""Say complete result?"")
|
||||
public void SayComplete()
|
||||
{
|
||||
Debug.Log(Complete);
|
||||
}
|
||||
}
|
||||
|
||||
var aType = typeof(A);
|
||||
//
|
||||
Debug.Log(aType.GetAttribute(typeof(DisplayNameAttribute));
|
||||
// DisplayNameAttribute
|
||||
Debug.Log(aType.GetAttribute<DisplayNameAttribute>());
|
||||
// DisplayNameAttribute
|
||||
|
||||
// also support MethodInfo、PropertyInfo、FieldInfo
|
||||
// 同时 也支持 MethodInfo、PropertyInfo、FieldInfo
|
||||
")]
|
||||
#endif
|
||||
public static T GetAttribute<T>(this Type type, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return type.GetCustomAttributes<T>(inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static object GetAttribute(this Type type, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return type.GetCustomAttributes(attributeType, inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static T GetAttribute<T>(this MethodInfo method, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return method.GetCustomAttributes<T>(inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static object GetAttribute(this MethodInfo method, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return method.GetCustomAttributes(attributeType, inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static T GetAttribute<T>(this FieldInfo field, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return field.GetCustomAttributes<T>(inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static object GetAttribute(this FieldInfo field, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return field.GetCustomAttributes(attributeType, inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static T GetAttribute<T>(this PropertyInfo prop, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return prop.GetCustomAttributes<T>(inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static object GetAttribute(this PropertyInfo prop, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return prop.GetCustomAttributes(attributeType, inherit).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4ad1fb17a7ff4016be06cd22911d23f9
|
||||
timeCreated: 1647484400
|
||||
Reference in New Issue
Block a user