增加Object扩展方法获取方法的调用堆栈。
This commit is contained in:
parent
bddca76f77
commit
ed43886481
|
@ -1,10 +1,6 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Falcon.SugarApi;
|
||||
|
||||
namespace Falcon.SugarApi.Test
|
||||
{
|
||||
|
@ -15,22 +11,22 @@ namespace Falcon.SugarApi.Test
|
|||
public void CloneToTest() {
|
||||
var s = new SourceClass { };
|
||||
var t = new TargetClass { };
|
||||
t.ica.ItemA="itemb";
|
||||
t.ica.ItemA = "itemb";
|
||||
|
||||
Assert.IsTrue(t.ica.ItemA=="itemb");
|
||||
Assert.IsTrue(t.ica.ItemA == "itemb");
|
||||
|
||||
var r = s.CloneTo(t);
|
||||
|
||||
Assert.IsNotNull(s);
|
||||
Assert.IsNotNull(r);
|
||||
Assert.IsNotNull(t);
|
||||
Assert.IsTrue(s.ia==t.ia,$"a.id:{s.ia},t.ia:{t.ia}");
|
||||
Assert.IsTrue(t.ia==1);
|
||||
Assert.IsTrue(s.sa==t.sa);
|
||||
Assert.IsTrue(s.sc=="sc");
|
||||
Assert.IsTrue(t.sd=="sd");
|
||||
Assert.IsTrue(s.ica.ItemA=="itema");
|
||||
Assert.IsTrue(t.ica.ItemA=="itema");
|
||||
Assert.IsTrue(s.ia == t.ia,$"a.id:{s.ia},t.ia:{t.ia}");
|
||||
Assert.IsTrue(t.ia == 1);
|
||||
Assert.IsTrue(s.sa == t.sa);
|
||||
Assert.IsTrue(s.sc == "sc");
|
||||
Assert.IsTrue(t.sd == "sd");
|
||||
Assert.IsTrue(s.ica.ItemA == "itema");
|
||||
Assert.IsTrue(t.ica.ItemA == "itema");
|
||||
Assert.IsTrue(t.ica.Equals(s.ica));
|
||||
}
|
||||
|
||||
|
@ -48,7 +44,7 @@ namespace Falcon.SugarApi.Test
|
|||
public void ThrowNullExceptionWhenNullTest() {
|
||||
var obj = new object();
|
||||
obj.ThrowNullExceptionWhenNull();
|
||||
obj=null;
|
||||
obj = null;
|
||||
Assert.ThrowsException<ArgumentNullException>(() => obj.ThrowNullExceptionWhenNull());
|
||||
}
|
||||
|
||||
|
@ -58,31 +54,31 @@ namespace Falcon.SugarApi.Test
|
|||
[TestMethod]
|
||||
public void ExtendPropertyTest() {
|
||||
var obj = new ExtendPropertyTestClass {
|
||||
Id=1,Name="Falcon",Sex=null,Count="count"
|
||||
Id = 1,Name = "Falcon",Sex = null,Count = "count"
|
||||
};
|
||||
var ep = obj.ExpandProperties();
|
||||
Assert.IsNotNull(ep);
|
||||
Assert.IsTrue(ep.Count()==4);
|
||||
Assert.IsTrue(ep.Select(m => m.Name).Distinct().Count()==4);
|
||||
Assert.IsTrue(ep.Count() == 4);
|
||||
Assert.IsTrue(ep.Select(m => m.Name).Distinct().Count() == 4);
|
||||
foreach(var p in ep) {
|
||||
if(p.Name=="Id") {
|
||||
Assert.IsTrue(p.Type==typeof(int));
|
||||
if(p.Name == "Id") {
|
||||
Assert.IsTrue(p.Type == typeof(int));
|
||||
Assert.IsTrue(p.Value.Equals(obj.Id));
|
||||
continue;
|
||||
}
|
||||
if(p.Name=="Name") {
|
||||
Assert.IsTrue(p.Type==typeof(string));
|
||||
if(p.Name == "Name") {
|
||||
Assert.IsTrue(p.Type == typeof(string));
|
||||
Assert.IsTrue(p.Value.Equals(obj.Name));
|
||||
continue;
|
||||
}
|
||||
if(p.Name=="Sex") {
|
||||
Assert.IsTrue(p.Type==typeof(string));
|
||||
Assert.IsTrue(p.Value==null);
|
||||
if(p.Name == "Sex") {
|
||||
Assert.IsTrue(p.Type == typeof(string));
|
||||
Assert.IsTrue(p.Value == null);
|
||||
continue;
|
||||
}
|
||||
if(p.Name=="Count") {
|
||||
Assert.IsTrue(p.Type==typeof(string));
|
||||
Assert.IsTrue(p.Value=="count");
|
||||
if(p.Name == "Count") {
|
||||
Assert.IsTrue(p.Type == typeof(string));
|
||||
Assert.IsTrue(p.Value == "count");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
@ -97,7 +93,7 @@ namespace Falcon.SugarApi.Test
|
|||
Assert.IsTrue(obj.IsNull());
|
||||
Assert.IsFalse(obj.IsNotNull());
|
||||
|
||||
obj=new object();
|
||||
obj = new object();
|
||||
Assert.IsTrue(obj.IsNotNull());
|
||||
Assert.IsFalse(obj.IsNull());
|
||||
|
||||
|
@ -105,10 +101,30 @@ namespace Falcon.SugarApi.Test
|
|||
Assert.IsTrue(obj1.IsNull());
|
||||
Assert.IsFalse(obj1.IsNotNull());
|
||||
|
||||
obj1=new object();
|
||||
obj1 = new object();
|
||||
Assert.IsTrue(obj1.IsNotNull());
|
||||
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>
|
||||
/// 扩展属性测试类
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MySqlX.XDevAPI.Common;
|
||||
using System;
|
||||
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.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
@ -141,6 +142,33 @@ namespace Falcon.SugarApi
|
|||
var ser = new JsonSerialize.JsonSerializeFactory().CreateJsonSerialize(option);
|
||||
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>
|
||||
|
|
Loading…
Reference in New Issue
Block a user