in delegatefunction can't call extension class method c#

인용 김 41 Reputation points
2022-03-27T10:52:31.843+00:00

187108-image.png

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?

C#
C#
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.
10,935 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,421 Reputation points
    2022-03-27T11:55:52.88+00:00

    Replace is for strings

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

    0 comments No comments

  2. 인용 김 41 Reputation points
    2022-03-27T14:31:38.203+00:00

    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 (SqlWork.com) 65,316 Reputation points
    2022-03-27T15:27:42.367+00:00

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.