C S h a r p 利 用 特 性 来 计 算 函 数 运 行 的 时 间 CSharp利用特性来计算函数运行的时间 CSharp利用特性来计算函数运行的时间
项目结构
用起来有点限制,目前只能用在的类里面的方法
第一步:编写类的装饰器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
namespace Test4Decropter
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class CalculateTimeAttribute : ContextAttribute, IContributeObjectSink
{
public CalculateTimeAttribute() : base("Decorator")
{
}
public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
{
return new Decorator(nextSink);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
namespace Test4Decropter
{
public class Decorator : IMessageSink
{
public Decorator(IMessageSink nextSink)
{
_NextSink = nextSink;
}
#region IMessageSink
private IMessageSink _NextSink;
public IMessageSink NextSink
{
get
{
return _NextSink;
}
}
public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
throw new NotImplementedException();
}
public IMessage SyncProcessMessage(IMessage msg)
{
IMessage result = null;
var call = msg as IMethodCallMessage;
if (call == null || Attribute.GetCustomAttribute(call.MethodBase, typeof(CalculateTimeMethodAttribute)) == null)
{
result = NextSink.SyncProcessMessage(msg);
}
else
{
var attr = Attribute.GetCustomAttribute(call.MethodBase, typeof(CalculateTimeMethodAttribute)) as CalculateTimeMethodAttribute;
Console.WriteLine(attr.PrefixPrint);
DateTime AlgorithmProStartTime = DateTime.Now;
Console.WriteLine("开始时间:"+ AlgorithmProStartTime.ToString());
result = NextSink.SyncProcessMessage(msg);
DateTime AlgorithmProEndTime = DateTime.Now;
Console.WriteLine("结束时间:" + AlgorithmProEndTime.ToString());
Console.WriteLine(attr.SuffixPrint);
Console.WriteLine("消耗时间:" + (AlgorithmProEndTime - AlgorithmProStartTime).ToString("ssfff"));
}
return result;
}
#endregion
}
}
第二步:编写方法的装饰器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test4Decropter
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class CalculateTimeMethodAttribute : Attribute
{
public CalculateTimeMethodAttribute()
{
PrefixPrint = "function start";
SuffixPrint = "function end";
}
public CalculateTimeMethodAttribute(String strPrefixPrint, String strSuffixPrint)
{
PrefixPrint = strPrefixPrint;
SuffixPrint = strSuffixPrint;
}
public String PrefixPrint { get; set; }
public String SuffixPrint { get; set; }
}
}
第三步:编写正常需要使用的类,需要继承ContextBoundObject
和使用第一步的类特性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test4Decropter
{
[CalculateTime]
public class SomeClass : ContextBoundObject
{
[CalculateTimeMethod]
public void SomeAction()
{
Console.WriteLine("do some Action");
Thread.Sleep(3000);
}
[CalculateTimeMethod(PrefixPrint = "begin the calculate", SuffixPrint = "end the calculate")]
public Int32 SomeCalculate(Int32 a, Int32 b)
{
var sum = a + b;
Console.WriteLine("do some Calculate:{0} plus {1} equals {2}", a, b, sum);
Thread.Sleep(3000);
return sum;
}
[CalculateTimeMethod]
public void test()
{
Console.WriteLine("hello major");
Thread.Sleep(3000);
}
}
}
第四步:主程序调用
在这里插入代码片```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test4Decropter
{
class Program
{
static void Main(string[] args)
{
var sc = new SomeClass();
sc.test();
sc.SomeAction();
Console.WriteLine();
sc.SomeCalculate(5, 8);
Console.Read();
}
}
}
全部代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test4Decropter
{
class Program
{
static void Main(string[] args)
{
var sc = new SomeClass();
sc.test();
sc.SomeAction();
Console.WriteLine();
sc.SomeCalculate(5, 8);
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test4Decropter
{
[CalculateTime]
public class SomeClass : ContextBoundObject
{
[CalculateTimeMethod]
public void SomeAction()
{
Console.WriteLine("do some Action");
Thread.Sleep(3000);
}
[CalculateTimeMethod(PrefixPrint = "begin the calculate", SuffixPrint = "end the calculate")]
public Int32 SomeCalculate(Int32 a, Int32 b)
{
var sum = a + b;
Console.WriteLine("do some Calculate:{0} plus {1} equals {2}", a, b, sum);
Thread.Sleep(3000);
return sum;
}
[CalculateTimeMethod]
public void test()
{
Console.WriteLine("hello major");
Thread.Sleep(3000);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
namespace Test4Decropter
{
public class Decorator : IMessageSink
{
public Decorator(IMessageSink nextSink)
{
_NextSink = nextSink;
}
#region IMessageSink
private IMessageSink _NextSink;
public IMessageSink NextSink
{
get
{
return _NextSink;
}
}
public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
throw new NotImplementedException();
}
public IMessage SyncProcessMessage(IMessage msg)
{
IMessage result = null;
var call = msg as IMethodCallMessage;
if (call == null || Attribute.GetCustomAttribute(call.MethodBase, typeof(CalculateTimeMethodAttribute)) == null)
{
result = NextSink.SyncProcessMessage(msg);
}
else
{
var attr = Attribute.GetCustomAttribute(call.MethodBase, typeof(CalculateTimeMethodAttribute)) as CalculateTimeMethodAttribute;
Console.WriteLine(attr.PrefixPrint);
DateTime AlgorithmProStartTime = DateTime.Now;
Console.WriteLine("开始时间:"+ AlgorithmProStartTime.ToString());
result = NextSink.SyncProcessMessage(msg);
DateTime AlgorithmProEndTime = DateTime.Now;
Console.WriteLine("结束时间:" + AlgorithmProEndTime.ToString());
Console.WriteLine(attr.SuffixPrint);
Console.WriteLine("消耗时间:" + (AlgorithmProEndTime - AlgorithmProStartTime).ToString("ssfff"));
}
return result;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
namespace Test4Decropter
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class CalculateTimeAttribute : ContextAttribute, IContributeObjectSink
{
public CalculateTimeAttribute() : base("Decorator")
{
}
public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
{
return new Decorator(nextSink);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test4Decropter
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class CalculateTimeMethodAttribute : Attribute
{
public CalculateTimeMethodAttribute()
{
PrefixPrint = "function start";
SuffixPrint = "function end";
}
public CalculateTimeMethodAttribute(String strPrefixPrint, String strSuffixPrint)
{
PrefixPrint = strPrefixPrint;
SuffixPrint = strSuffixPrint;
}
public String PrefixPrint { get; set; }
public String SuffixPrint { get; set; }
}
}
参考文档:
https://blog.csdn.net/aimanbing4182/article/details/101327320
https://www.zhihu.com/question/36211661?sort=created