简体   繁体   中英

c# Func Delegate - how to pass the value in generics

I would like to use the Func in an interface - Generics . based on the input i would like to process the operation. But somehow I am not able to get the input in the function .

1) What should be the use case where I shall pass Func<> as a parameter? 2) Whats the advantage of returning Fuc<> as a return object ?

I created an interface .

 public interface IAutoFacDemo
 {
    Task<string> GetAsync(Func<string, Task<string>> concat, string input);
 }

Which has a concrete implementation of

public class BusConcat : IAutoFacDemo
{
    #region Implementation of IAutoFacDemo

    public BusConcat()
    {

    }

// ?? I am not able to extract the input string "dinesh" where from the program // I passed it as an lambda expression

    public async Task<string> GetAsync(Func<string, Task<string>> concat, string input)
    {
        var v = Function(concat, input).Result;
        return (v);
    }

    private static async Task<string> Function(Func<string, Task<string>> bus, string input)
    {
        var v = await bus(Process("").Result).ConfigureAwait(false);
        return  v;
    }

    private static async Task<string> Process(string input)
    {
        return Task.FromResult( ":").Result;
    }
    #endregion
}

Then to consume the interface i created another interface for Autofac module load

public interface IFactory
{
    Task<string> GetValue(Func<string,Task<string>> fn,string input);
}

And from the main

 static void Main(string[] args)
    {
        var scope = AutoFacContainerConfig.RegisterLifetimeScope();
        {
            using (var resolveEntity = scope.BeginLifetimeScope())
            {
                var names =new List<string>{"dinesh","tripathi","avenue"};

                var factory = resolveEntity.Resolve<IFactory>();
                Console.WriteLine(factory.GetType().ToString());
                var x = string.Empty;
                var v=(x.Insert(0, names.First(xc => xc.Contains("dinesh"))));

// I am trying to call the Func<string,Task<string>> where i want to pass 
//the list if it contains "dinesh" to the function and based on the string 
//it will process the function 

                var xx = factory.GetValue(async i=>i+await Task.FromResult(names.First(x1=>x1.Contains("dinesh"))),"Test").Result;
                Console.WriteLine(xx);
            }
        }
    }

I am getting dinesh:Test as an output , but i want to handle "dinesh" when passed the string as a lambda value to the Func

Great. Got it . To get the lambda value "dinesh" in the Function GetAsync

I declared string variable and called Invoke on Concat to get the reult.

string ret=string.empty
var retVal=concat.Invoke(ret).Result 

Got me "dinesh" as passed from the lambda expression.

Thanks all. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM