Simplified expression selection

I am currently using this code in class i, which is called "Provide", which is essentially the class of static method labels that I use to make throw exceptions easier, so I don’t have to write at least 3 to make an exception all the time, all this can always be done on 1 line.

    [DebuggerHidden, DebuggerStepThrough]
    public static void ArgumentNotNull(object argument, string name)
    {
        if (argument == null)
        {
            throw new ArgumentNullException(name, "Cannot be null");
        }
    }

    [DebuggerHidden, DebuggerStepThrough]
    public static void ArgumentNotNull<T>(Expression<Func<T>> expr)
    {
        var e = (MemberExpression)expr.Body;

        var val = GetValue<T>(e);

        ArgumentNotNull(val, e.Member.Name);
    }

My problem is that when Ensure.ArgumentNotNullI call, I must either do:

Ensure.ArgumentNotNull(arg, "arg");

or

Ensure.ArgumentNotNull(() => arg);

Since I need a name in order to be able to explain which argument caused the exception in the exception, its me.

Is there a way to make a call ArgumentNotNullwithout needing a () =>lambda part , and just call Ensure.ArgumentNotNull(arg)and still be able to get the name of the passed argument, without having to pass the name as well.

+4
4

ArgumentNotNull () => Ensure.ArgumentNotNull(arg)

, , , , . - Ensure.ArgumentNotNull(null);.

+2

public static void ArgumentNotNull(object argument)
    {
        StackFrame stackFrame = new StackTrace(true).GetFrame(1);
        string fileName = stackFrame.GetFileName();
        int lineNumber = stackFrame.GetFileLineNumber();
        var file = new System.IO.StreamReader(fileName);
        for (int i = 0; i < lineNumber - 1; i++)
            file.ReadLine();
        string varName = file.ReadLine().Split(new char[] { '(', ')' })[1];


        if (argument == null)
        {
            throw new ArgumentNullException(varName, "Cannot be null");
        }
    }

OP

  'and still be able to get the name of the argument that was passed, without having to specifically pass the name as well.'

lamdba .

myObject.ArgumentNotNull(x=>x.SomeProperty);  -- prop checking
myObject.ArgumentNotNull(x=>x);  -- objchecking

[DebuggerHidden, DebuggerStepThrough]
    public static void ArgumentNotNull<T>(this T obj, Expression<Func<T, object>> expr = null)
    {
        if (obj == null) throw new NullReferenceException();

        var body = expr.Body as MemberExpression;

        if (body == null)
        {
            var ubody = (UnaryExpression)expr.Body;
            body = ubody.Operand as MemberExpression;
        }

        if (body != null)
        {
            var property = body.Member as PropertyInfo;
            if (property == null) throw;
            if (obj.GetType().GetProperty(property.Name).GetValue(obj, null) == null) throw new NullReferenceException();

        }
        else
        {
            var ubody = (UnaryExpression)expr.Body;
            var property = ubody.Operand as MemberExpression;
            if (property != null)
                props[property.Member.Name] = obj.GetType()
                    .GetProperty(property.Member.Name)
                    .GetValue(obj, null);
            if (obj.GetType().GetProperty(property.Member.Name).GetValue(obj, null) == null) throw new NullReferenceException();

        }

    }
+1

Fody.NullGuard , throw new ArgumentNullException("argName");.

Fody.NullGuard,

public class Sample
{
    public void SomeMethod(string arg)
    {
        // throws ArgumentNullException if arg is null.
    }

    public void AnotherMethod([AllowNull] string arg)
    {
        // arg may be null here
    }
}

Fody IL , ,

public class Sample
{
    public void SomeMethod(string arg)
    {
        if(arg == null) 
            throws new  ArgumentNullException("arg");
    }

    public void AnotherMethod(string arg)
    {
    }
}
0

- , - , Fody.NullGuard, Aron ( ) AOP (PostSharp).

, , . , :

  • API
  • # .

API :

void SomeMethod(object arg1, string arg2, List<int> arg3)
{
    new { arg1, arg2, arg3 }.ShouldNotBeNull();
    ....

The implementation is too large to show here, so it can be found in this value . In addition, it can be expanded to check range, etc.

0
source

All Articles