问题
Looking for a way to check if an string contains in another ignoring upper/lower case, I found it:
Works fine. Then, I tried put it to my StringExtensions
namespace.
namespace StringExtensions
{
public static class StringExtensionsClass
{
//...
public static bool Contains(this string target, string toCheck, StringComparison comp)
{
return target.IndexOf(toCheck, comp) >= 0;
}
}
}
and then:
using StringExtensions;
...
if (".. a".Contains("A", StringComparison.OrdinalIgnoreCase))
but I get the following error:
No overload for method 'Contains' takes '2' arguments
How do I fix it?
回答1:
When you want to use your extension, add this using statement:
using StringExtensions;
Because of the way Extension methods are declared, visual studio won't find them by itself, and the regular Contains
method takes one argument, hence your exception.
回答2:
I found my mistake:
for this works with dynamic
type need use a cast
to string
. .ToString()
method is not sufficient.
if (((string)result.body).Contains(foo, StringComparison.OrdinalIgnoreCase))
Works fine now. Thanks again stackoverflow. :)
来源:https://stackoverflow.com/questions/8345598/cant-access-my-extension-method