Can't Access My Extension Method

ぐ巨炮叔叔 提交于 2019-12-23 07:03:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!