增加Object扩展方法获取方法的调用堆栈。
This commit is contained in:
parent
bddca76f77
commit
ed43886481
|
@ -1,10 +1,6 @@
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Falcon.SugarApi;
|
|
||||||
|
|
||||||
namespace Falcon.SugarApi.Test
|
namespace Falcon.SugarApi.Test
|
||||||
{
|
{
|
||||||
|
@ -109,6 +105,26 @@ namespace Falcon.SugarApi.Test
|
||||||
Assert.IsTrue(obj1.IsNotNull());
|
Assert.IsTrue(obj1.IsNotNull());
|
||||||
Assert.IsFalse(obj1.IsNull());
|
Assert.IsFalse(obj1.IsNull());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取调用堆栈测试
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void GetStackTraceTest() {
|
||||||
|
var r = this.GetStackTrace().ToArray();
|
||||||
|
Assert.IsNotNull(r);
|
||||||
|
foreach(var i in r) {
|
||||||
|
Console.WriteLine(i.Name);
|
||||||
|
}
|
||||||
|
Assert.IsTrue(r.Count() > 0);
|
||||||
|
Assert.IsTrue(r[0].Name == "GetStackTrace");
|
||||||
|
Assert.IsTrue(r[1].Name == "GetStackTraceTest");
|
||||||
|
|
||||||
|
var fullCount = this.GetStackTrace(full: true).Count();
|
||||||
|
var nFullCount = this.GetStackTrace(full: false).Count();
|
||||||
|
Assert.IsTrue(fullCount > nFullCount);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 扩展属性测试类
|
/// 扩展属性测试类
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using MySqlX.XDevAPI.Common;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
|
35
Falcon.SugarApi.Test/TypeExtendTest.cs
Normal file
35
Falcon.SugarApi.Test/TypeExtendTest.cs
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace Falcon.SugarApi.Test
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 类型扩展相关测试
|
||||||
|
/// </summary>
|
||||||
|
[TestClass]
|
||||||
|
public class TypeExtendTest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前执行方法测试
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void GetMethod() {
|
||||||
|
var method = System.Reflection.MethodBase.GetCurrentMethod();
|
||||||
|
Console.WriteLine(method.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前方法调用堆栈
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void GetStackTrace() {
|
||||||
|
var trace = new StackTrace();
|
||||||
|
for(int i = 0;i < trace.FrameCount;i++) {
|
||||||
|
var frame = trace.GetFrame(i);
|
||||||
|
var method = frame.GetMethod();
|
||||||
|
Console.WriteLine($"{method.Name} in {method.DeclaringType.FullName}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
@ -141,6 +142,33 @@ namespace Falcon.SugarApi
|
||||||
var ser = new JsonSerialize.JsonSerializeFactory().CreateJsonSerialize(option);
|
var ser = new JsonSerialize.JsonSerializeFactory().CreateJsonSerialize(option);
|
||||||
return ser.Serialize(obj);
|
return ser.Serialize(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前方法开始向上的调用堆栈。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="_">一个对象。可以为任何对象,没有实际意义,可以直接使用this</param>
|
||||||
|
/// <param name="startLevel">开始级别。0为当前方法。默认0</param>
|
||||||
|
/// <param name="full">是否需要完整调用堆栈</param>
|
||||||
|
/// <returns>方法的调用堆栈枚举</returns>
|
||||||
|
public static IEnumerable<MethodBase> GetStackTrace(this object _,int startLevel = 0,bool full = false) {
|
||||||
|
var trace = new StackTrace();
|
||||||
|
var r = new List<MethodBase>();
|
||||||
|
for(int i = startLevel;i < trace.FrameCount;i++) {
|
||||||
|
var frame = trace.GetFrame(i);
|
||||||
|
if(frame == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(!full && frame.GetILOffset() == StackFrame.OFFSET_UNKNOWN) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var method = frame.GetMethod();
|
||||||
|
if(method == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
r.Add(method);
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user