【问题标题】:How to create an aspect checking for null references on all methods in a class in postsharp如何在postsharp中创建一个方面检查类中所有方法的空引用
【发布时间】:2008-11-04 07:26:56
【问题描述】:

如何在 postsharp 中创建一个方面检查类中所有方法的空引用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    [MethodParameterNullCheck]
    internal class Class
    {
        public Class()
        {

        }

        public void MethodA(int i, ClassA a, ClassB b)
        {
              //Some business logic
        }
    }
}

[MethodParameterNullCheck] 方面应展开为以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    [MethodParameterNullCheck]
    internal class Class
    {
        public Class()
        {

        }

        public void MethodA(int i, ClassA a, ClassB b)
        {
            if (a == null) throw new ArgumentNullException("Class->MethodA: Argument a of ClassA is not allowed to be null.");
            if (b == null) throw new ArgumentNullException("Class->MethodA: Argument b of ClassB is not allowed to be null.");
            // Some Business Logic
        }
    }
}

如果您能给我一个示例实现,让我开始使用 postsharp 进行 AOP,我将不胜感激。

【问题讨论】:

    标签: c# .net postsharp


    【解决方案1】:

    另一种方法是扩展方法:

    public static void ThrowIfNull<T>(this T obj, string parameterName) where T : class
    {
        if(obj == null) throw new ArgumentNullException(parameterName);
    }
    

    然后调用:

    foo.ThrowIfNull("foo");
    bar.ThrowIfNull("bar");
    

    T : class 使我们不小心装箱等。

    重新 AOP; Jon Skeet 有一个类似 here 的示例,但涵盖了一个方法/参数。

    这是复制的方面;请注意,此方面一次仅涵盖 1 个参数,并且是特定于方法的,但总的来说,我认为这是完全合理的……但是,您可能可以更改它。

    using System;
    using System.Reflection;
    using PostSharp.Laos;
    
    namespace IteratorBlocks
    {
        [Serializable]
        class NullArgumentAspect : OnMethodBoundaryAspect
        {
            string name;
            int position;
    
            public NullArgumentAspect(string name)
            {
                this.name = name;
            }
    
            public override void CompileTimeInitialize(MethodBase method)
            {
                base.CompileTimeInitialize(method);
                ParameterInfo[] parameters = method.GetParameters();
                for (int index = 0; index < parameters.Length; index++)
                {
                    if (parameters[index].Name == name)
                    {
                        position = index;
                        return;
                    }
                }
                throw new ArgumentException("No parameter with name " + name);
            }
    
            public override void OnEntry(MethodExecutionEventArgs eventArgs)
            {
                if (eventArgs.GetArguments()[position] == null)
                {
                    throw new ArgumentNullException(name);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      相关资源
      最近更新 更多