argumentnullexception

Create class instance from string

混江龙づ霸主 提交于 2019-12-01 04:23:07
问题 I have a C# method which creates a new instance of a class from a string, however, I get an error when running the code. obj = (ClassX)Activator.CreateInstance(Type.GetType("classPrefix_" + className)); ArgumentNullException was unhandled Value cannot be null Parameter name: type Any help on this error would be appreciated. 回答1: You may need to use the assembly qualified name as the argument to Type.GetType eg AssemblyName.Namespace.ClassName MSDN Doc on assembly qualified names 回答2: You may

Why does this string extension method not throw an exception?

对着背影说爱祢 提交于 2019-11-29 20:13:02
I've got a C# string extension method that should return an IEnumerable<int> of all the indexes of a substring within a string. It works perfectly for its intended purpose and the expected results are returned (as proven by one of my tests, although not the one below), but another unit test has discovered a problem with it: it can't handle null arguments. Here's the extension method I'm testing: public static IEnumerable<int> AllIndexesOf(this string str, string searchText) { if (searchText == null) { throw new ArgumentNullException("searchText"); } for (int index = 0; ; index += searchText

Design by contract/C# 4.0/avoiding ArgumentNullException

╄→尐↘猪︶ㄣ 提交于 2019-11-29 14:58:31
问题 I'm terribly tired of checking all my arguments for null, and throwing ArgumenutNullExceptions when they are. As I understand it, C# 4.0 enables some design by contract constructs. Will it be possible to specify that a method will not accept null arguments in C# 4.0? Also, is there anything I can do in the meantime (maybe an attribute?) to avoid this monotonous task of checking for null and throwing? 回答1: You can create a NotNull<T> generic class that helps, but there are some side effects.

How do I Unit Test Actions without Mocking that use UpdateModel?

人盡茶涼 提交于 2019-11-27 16:29:49
问题 I have been working my way through Scott Guthrie's excellent post on ASP.NET MVC Beta 1. In it he shows the improvements made to the UpdateModel method and how they improve unit testing. I have recreated a similar project however anytime I run a UnitTest that contains a call to UpdateModel I receive an ArgumentNullException naming the controllerContext parameter. Here's the relevant bits, starting with my model: public class Country { public Int32 ID { get; set; } public String Name { get;