What is the smoothest, most appealing syntax you've found for asserting parameter correctness in c#?

后端 未结 14 1850
一个人的身影
一个人的身影 2020-12-22 23:50

A common problem in any language is to assert that parameters sent in to a method meet your requirements, and if they don\'t, to send nice, informative error messages. This

相关标签:
14条回答
  • 2020-12-23 00:26

    Postsharp or some other AOP framework.

    0 讨论(0)
  • 2020-12-23 00:27

    Using my library The Helper Trinity:

    public void SomeMethod(string firstName, string lastName, int age)
    {
        firstName.AssertNotNull("firstName");
        lastName.AssertNotNull("lastName");
    
        ...
    }
    

    Also supports asserting that enumeration parameters are correct, collections and their contents are non-null, string parameters are non-empty etcetera. See the user documentation here for detailed examples.

    0 讨论(0)
  • 2020-12-23 00:29

    Wow, I found something really interesting here. Chris above gave a link to another Stack Overflow question. One of the answers there pointed to a blog post which describes how to get code like this:

    public static void Copy<T>(T[] dst, long dstOffset, T[] src, long srcOffset, long length)
    {
        Validate.Begin()
                .IsNotNull(dst, “dst”)
                .IsNotNull(src, “src”)
                .Check()
                .IsPositive(length)
                .IsIndexInRange(dst, dstOffset, “dstOffset”)
                .IsIndexInRange(dst, dstOffset + length, “dstOffset + length”)
                .IsIndexInRange(src, srcOffset, “srcOffset”)
                .IsIndexInRange(src, srcOffset + length, “srcOffset + length”)
                .Check();
    
        for (int di = dstOffset; di < dstOffset + length; ++di)
            dst[di] = src[di - dstOffset + srcOffset];
    }
    

    I'm not convinced it is the best answer yet, but it certainly is interesting. Here's the blog post, from Rick Brewster.

    0 讨论(0)
  • 2020-12-23 00:30

    You should check out Code Contracts; they do pretty much exactly what you're asking. Example:

    [Pure]
    public static double GetDistance(Point p1, Point p2)
    {
        CodeContract.RequiresAlways(p1 != null);
        CodeContract.RequiresAlways(p2 != null); 
        // ...
    }
    
    0 讨论(0)
  • 2020-12-23 00:31

    This may be somewhat helpful:

    Design by contract/C# 4.0/avoiding ArgumentNullException

    0 讨论(0)
  • 2020-12-23 00:31

    It does not apply everywhere, but it might help in many cases:

    I suppose that "SomeMethod" is carrying out some behavioral operation on the data "last name", "first name" and "age". Evaluate your current code design. If the three pieces of data are crying for a class, put them into a class. In that class you can also put your checks. This would free "SomeMethod" from input checking.

    The end result would be something like this:

    public void SomeMethod(Person person)
    {
        person.CheckInvariants();
        // code here ...
    }
    

    The call would be something like this (if you use .NET 3.5):

    SomeMethod(new Person { FirstName = "Joe", LastName = "White", Age = 12 });
    

    under the assumption that the class would look like this:

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    
        public void CheckInvariants()
        {
            assertNotNull(FirstName, "first name");
            assertNotNull(LastName, "last name");
        }
    
        // here are your checks ...
        private void assertNotNull(string input, string hint)
        {
            if (input == null)
            {
                string message = string.Format("The given {0} is null.", hint);
                throw new ApplicationException(message);
            }
        }
    

    Instead of the syntactic sugar of .NET 3.5 you can also use constructor arguments to create a Person object.

    0 讨论(0)
提交回复
热议问题