Is it possible to refactor this extension method?

后端 未结 6 803
小鲜肉
小鲜肉 2020-12-24 07:40

I have the following extension method:

public static void ThrowIfArgumentIsNull(this T value, string argument) 
    where T : class
{
    if (value          


        
6条回答
  •  太阳男子
    2020-12-24 08:31

    See also ArgumentNullException and refactoring for a complete solutions along the same lines as the answer.

    What about:

    public void Save(Category qwerty)
    {   
       ThrowIfArgumentIsNull( () => return qwerty );
       qwerty.ThrowIfArgumentIsNull("qwerty");    
       // ....
    }
    

    then define ThrowIfArgumentIsNull as

    public static void ThrowIfArgumentIsNull(Expression> test)
    {
       if (test.Compile()() == null)
       {
          // take the expression apart to find the name of the argument
       }
    }
    

    sorry I don't have the time to fill in the detail or provide the full code at present.

提交回复
热议问题