user172632
user172632

Reputation:

How do I apply a String extension to a Func<String>

I have a constructor signature like this

public NavigationLink(Func<String> getName, 
                      Func<UrlHelper, String> getURL, 
                      Func<bool> isVisible, 
                      IEnumerable<NavigationLink> subItems)

Inside that constructor I am assigning the getName to a property of the containing class

GetName = getName

I have a string extension that does casing on a string call String.CapitalizeWords();

How do I apply that extension to the Func?

Thanks

Upvotes: 2

Views: 499

Answers (4)

bottlenecked
bottlenecked

Reputation: 2157

So is this the signature for the GetName property?

string GetName{get;set;}

If so, I guess all you need to do is

GetName=getName().CapitalizeWords();

Otherwise if the signature is like this:

Func<string> GetName{get;set;}

you'd probably need to do

GetName=getName;
string caps = GetName().CapitalizeWords();

I hope I understood your question :P

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039588

GetName = () => getName().CapitalizeWords();

Upvotes: 0

Patrick Karcher
Patrick Karcher

Reputation: 23613

It will be there for the results of running getName, not on getName itself. GetName is a delegate to a function that will return a string; it is not itself a string. You should be able to do String CapName = GetName().CapitalizeWords().

Upvotes: 0

Jens
Jens

Reputation: 25593

You could do

GetName = () => getName().CapitalizeWords();

But why do you need a parameterless function, returning a String instead of the String itself?

Upvotes: 3

Related Questions