调用带有Attribute的函数
本文关键字:函数 Attribute 调用 | 更新日期: 2023-09-27 18:02:15
我有一个函数,该函数使用秒表实例通过启动和停止来测量方法的时间。我能不能在属性中定义这个函数然后用这个属性修饰任何方法来测量这个方法的时间?这将减少实际控制线。我不想使用任何第三方库。
public class AppTrace:BaseModel<AppTrace>
{
[DataMember]
public string Comment;
[DataMember]
public string MethodName;
[DataMember]
public DateTime StartTimeStamp;
[DataMember]
public int Duration;
[DataMember]
public int UserObjectId;
[DataMember]
public string MachineName;
[DataMember]
public int ToolId;
private System.Diagnostics.Stopwatch stpWatch;
public AppTrace(string comment,string methodName,int userObjectId ,string machineName = "",int? toolId=null)
{
MethodName = methodName;
UserObjectId = userObjectId;
StartTimeStamp = DateTime.UtcNow;
Comment = comment;
MachineName = machineName;
stpWatch = new System.Diagnostics.Stopwatch();
stpWatch.Start();
}
public AppTrace()
{
}
public void CloseTrace()
{
this.stpWatch.Stop();
Duration=Convert.ToInt32( this.stpWatch.ElapsedMilliseconds);
}
}
如果不是属性,我可以在委托的帮助下做到这一点吗?
正如我在评论中所说,这通常是不可能的。您可以使用一些AOP框架,它可以相当成功地处理这些事情。或者编写一些调用实用程序。
在最简单的情况下,您的实用程序可以像这样:
public static class Invoke
{
public static TimeSpan Measure(Action action)
{
if (action == null)
{
throw new ArgumentNullException();
}
var stopWatch = Stopwatch.StartNew();
action();
stopWatch.Stop();
return stopWatch.Elapsed;
}
}
那么您将能够度量方法调用时间。例如,将使用Foo
类作为测量目标。
// don't use such naming in production code
public class Foo
{
public void DoWork1()
{
// imitating hard computations
Thread.Sleep(TimeSpan.FromSeconds(5));
}
public void DoWork2(int param1)
{
// imitating hard computations
Thread.Sleep(param1);
}
}
public static class Program
{
public static void Main()
{
var foo = new Foo();
// signature of foo.DoWork1 is suitable for Invoke.Measure,
//so you can use it as MethodGroup
var time1 = Invoke.Measure(foo.DoWork1);
Console.WriteLine("Time for 'DoWork1' is {0}s", time1.TotalSeconds);
// signature of foo.DoWork2 is not suitable for Invoke.Measure,
// so you have to call it inside additional lambda
var time2 = Invoke.Measure(() => foo.DoWork2(42));
Console.WriteLine("Time for 'DoWork2' is {0}s", time2.TotalSeconds);
}
}
对于返回一些值,但也可以实现的方法来说,这将更加困难。添加辅助类Timed<T>
,包含返回值和操作持续时间。
public class Timed<T>
{
public readonly TimeSpan Duration;
public readonly T Result;
// making constructor private to force static factories usage
private Timed(T result, TimeSpan duration)
{
Result = result;
Duration = duration;
}
// static factory method instead of constructor, for generic parameter
// type inference
public static Timed<T> Create(T result, TimeSpan duration)
{
return new Timed<T>(result, duration);
}
}
则需要Invoke.Measure
过载。
public static class Invoke
{
// Func<T> instead of Action
public static Timed<T> Measure(Func<T> func)
{
if (func== null)
{
throw new ArgumentNullException();
}
var stopWatch = Stopwatch.StartNew();
var result = func();
stopWatch.Stop();
return Timed.Create(result, stopWatch.Elapsed);
}
}
通过F# REPL
有#time
指令来测量调用时间。你可以从F#
调用任何.NET
代码。它被集成到Visual Studio中,或者可以从命令行调用(fsi.exe
)。我不知道你的实际目标,所以它可能是合适的。