I was checking the facebook sdk and came accross this function:
FB.ShareLink(
new Uri("https://developers.facebook.com/"),
callback: ShareCallback
);
My question is, does the callback: means?, Is it stating that parameter should be linked with that callback? if so what is the benefit of this?
callback: is called "named parameter". It means that in the sharelink, there is a named parameter called as "callback" and you fill the callback parameter with ShareCallback.
Named parameters are very useful if there are a lot of overload parameters, and we want to make sure that the parameter name that we use is the correct one.
This might help you understand more.
This is called a named parameter. It can be used to more clearly document what your code is doing, or to call a method where some of the parameters have default values without specifying all the parameters explicitly.
Related
I have a method that accepts an Action with a signature like:
public void DoSomething(Action code);
then I would call this method like:
DoSomething(()=> CallSomething("A","B","C");
In the DoSomething method, how can I get the argument values?
Another thing to consider is that CallSomething can potentially have optional parameters, meaning I can't just change the signature of DoSomething to -> Expression<Action>
All I need to do is get the argument values, I'm interested in any method that can work.
I've already tried to create a new method which accepts Expression<Action> then pass the Action through (From DoSomething), but the arguments weren't populated.
Any suggestions welcome.
The point is you don't want to.
Your method accepts an Action delegate. It's saying "I only want something to call that has no arguments and returns no value".
If you need to pass arguments to the function, either use a different delegate, or pass them as arguments directly.
Think about this without the lambda. You have an interface that has a single method, Act that takes no arguments and returns no value.
DoSomething(IMyInterface myInterface)
{
myInterface.Act(); // How do I get the arguments of some method that `Act` calls?
}
Does this make any sense whatsoever? Would you expect to be able to disassemble the whole instance that's passed in myInterface, find its Act method, go through all of the methods that are called from there and intercept their arguments? Delegates are little but a single-method interface with a bit of state carried over.
If you need the arguments, make them part of the public interface. Otherwise, your abstraction has no meaning whatsoever, and you shouldn't be using it. Nobody is forcing you to use lambdas, and nobody is forcing you to pass a single lambda and no arguments, and nobody is forcing you to use Action in particular. They are all simple tools - if they don't fit your task, don't use them.
To answer your question more directly: you get the arguments as any other arguments. The problem is that your action has no arguments.
I made a method to loop and clear all textbox controls in my form.
Controls.OfType<TextBox>()
.ToList()
.ForEach(tb => tb.Clear());
This works just fine, but I figured that since the first argument passed to any instance method is always a reference to the instance that I should be able to write it like this
Controls.OfType<TextBox>()
.ToList()
.ForEach(TextBox.Clear);
Unfortunately that doesn't actually work, and I don't quite understand why..
It would work if TextBox.Clear was a static method with a TextBox parameter; but instead, it's an instance method with no parameters, so the compiler can't automatically transform it to an Action<TextBox>.
Note that the CLR does support open-instance delegates (you can create one with the Delegate.CreateDelegate method), but the C# language doesn't support it.
Here's how to create an open-instance delegate that will invoke TextBox.Clear on its argument:
var action = (Action<TextBox>)Delegate.CreateDelegate(
typeof(Action<TextBox>),
null,
typeof(TextBox).GetMethod("Clear"));
The this parameter is implicit, not explicit. Foreach is expecting a method with an explicit parameter, not an implicit one.
As for why the C# language team didn't implement this feature, you'll have to ask them. They of course could have designed the language to support this, if they wanted to. There's no real point in us speculating as to why they didn't.
I was hoping to get some clarity on the use of () before the call of a method (applies to class,struc, etc as well). Firstly I don't know what the action is called so having trouble in pinpointing where to start reading on it, and secondly if you have some explanation on why one would use this that would be great.
In the example below I am interested in the (TestClassRemoting) that is called in front of the Activator.Getobject(...) method.
Example
TestClassRemoting test = (TestClassRemoting)Activator.GetObject(someType,someString);
Now in the scripts, TestClassRemoting is just a class defined with a Text method that will print a string.
in languages like c# and java explicit type conversions are done by specifying the type the data should be converted to inside () brackets. so in your case the object returned by the Activator.GetObject is converted to type TestClassRemoting.
find more info here,
http://en.wikipedia.org/wiki/Type_conversion
An expression tree may not contain a call or invocation that uses
optional arguments
return this.RedirectToAction<MerchantController>(x => x.Edit(merchantId));
Where edit had a second, nullable argument.
Why is this?
Had the same message when trying to use Mock.setup to mock a method with multiple default parameters. I just had to add the additional parameters in the lambda.
void someMethod(string arg1 = "", string arg2 = "")
mockedObject.Setup(x => x.someMethod(It.IsAny<string>(), It.IsAny<string>()))
The underlying expression tree API does not support optional arguments.
For IL-compiled code the C# compiler inserts the default values at compile time (hard-coded), because the CLR does not support calling methods with optional arguments either when the arguments are not provided explicitly.
Error: 'an exception tree may not contain a call or invocation that uses option arguments'
Why: Because you are not providing the optional parameters when calling the method. Mainly you get this with .net core when using IAsyncProxy service object.
Fix: Pass all the optional parameters value, you may use default value if you.
I dealt with it by adding the optional parameter with a value . It worked. The same thing happened when I tried mocking while doing a setup .
You might want to test that a method that has default parameters is called without any argument passed, in that case:
myMock.someMethod(default,default)
can work
I want to pass a method around and store it. Let's say the method takes one int and returns void. I'd wrap it in Action< int >.
Now, is there a way to invoke that method but not on the object it originated from but a different instance of the same class?
It's kind of hard to be more specific than that but please ask if anything is confusing and I'll try again.
Now, is there a way to invoke that method but not on the object it originated from but a different instance of the same class?
EDIT: Okay, ignore my first attempt. I thought you were talking about a different class.
It sounds like you should actually be wrapping the method in an Action<object, int> (or a more strongly typed version).
Alternatively, you could create a new delegate using:
Action<int> newAction = (Action<int>) Delegate.CreateDelegate(typeof(Action<int>),
newTarget,
oldAction.Method);
You can use the Type.GetMethod overload which takes parameter types. This allows you to get the exact method with the specified parametere types.