in delegatefunction can't call extension class method c#

인용 김 41 Reputation points


I tried to call extension string extension function , if not in delegatefunction it works well, but
in delegate function can't call extension method

then how to call extension method in delegate function?

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,502 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 34,671 Reputation points

    Replace is for strings

    new Random().NextDouble().ToString().Replace( . . .

    0 comments No comments

  2. 인용 김 41 Reputation points

    ah sorry i found a why it is not worked but i don't have further idea how i can solve it

    "jQuery" + ((func2_version + new Random().NextDouble())).replace(new RegExp("\D", "g"), "");

    in here func2_version is dynamic type ,

    runtimebinder knew (func2_version+new Random().NextDoube()) is type of string

    but it is strangely can't find string's extension method

    if I specify string type cast it works .....

    "jQuery" + ((string)(func2_version + new Random().NextDouble())).replace(new RegExp("\D", "g"), "");

    but I want a way of not specifying cast

    0 comments No comments

  3. Bruce ( 49,051 Reputation points

    The compiler needs to know the datatype of a variable to pick the proper class extension. The point of a dynamic is the datatype is not known to runtime. This means you can not use a string extension on a dynamic.

    As your replace extension method only works if the dynamic is a string (otherwise it throws an error), you should be able cast.

    A better solution is to convert the result of the + operation to a string

    "jQuery" + (func2_version + new Random().NextDouble()).ToString().replace(new RegExp("\\D", "g"), "");
    0 comments No comments