Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I use C#.
When I defined Hoge method below,
void Hoge(bool isBar){}
I get the Hoge method like below
var methodName = this.Hoge as Action<bool>).Method.Name;
However, I can't understand what does this.Hoge type.
Because, it can assign and casting.
but, it can't give me method name directly.
this.Hoge.Method.Name;
and, it also error. typeof(this.Hoge)
what is method variable exactly?
The code you provided isn't valid C# code, so it's very difficult to understand what you're asking. But I think you're trying to understand how the expression this.Hoge is translated into something that can eventually provide you with the name of the method.
If so, then your code example should look something like this:
var methodName = ((Action<bool>)this.Hoge).Method.Name;
And what that does is to implicitly create an instance of a delegate type (in this case, of the type Action<bool>), as if you'd written this:
var methodName = new Action<bool>(this.Hoge).Method.Name;
And of course, once you have a delegate type, that type has a Method property, which returns a MethodInfo object which in turn, of course, has a Name property.
If that is not what you're asking, please improve your question by providing a valid, compilable C# example of what you're asking about, along with a more precisely worded question about that code.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I would like to create a class or struct for this message, but I don't know what the following means:
body:,from:,to:,
I know what they are doing on a high-level, but don't know what these words are for or how they would fit into a class.
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
body: "Join Earth's mightiest heroes. Like Kevin Bacon.",
from: new Twillio.Types.PhoneNumber("+1501712261"),
to: new Twillio.Types.PhoneNumber("+1501712261")
)
This is a named arguments of methods. Basically, you can specify an argument for a parameter by associating the argument with the parameter's name without keeping in mind the order of arguments in parameters list. You can read more at MSDN
Named arguments free you from the need to remember or to look up the
order of parameters in the parameter lists of called methods. The
parameter for each argument can be specified by parameter name.
the body:, from: ? It's the name of the parameters, it makes the call more readable, it allows you provide the arguments in another order, and it has no influence on the performance, it's just syntax sugar.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
edit Hmm, I don't believe this is a duplicate of other questions asking about var. I am asking about Reflection, and just curious if it should be used sparingly like var
Also an extra shout out to Flydog57 for their responses in comments, I appreciate them~ edit
Forgive me if my question is improper, first time posting my own. I am trying to be more proper with my code while also trying to get things I've never tried before to work~
Playing around with code seeing what I can and can't do. Even if it has no real point or purpose. Just using it as a way to entertain myself and learn as I go.
My current project is having one script add more lines of code to a different file. I thought about this when I realized that code is just text, and you can make code that adds text to a file.
So I was thinking of code that would write new methods that can be called. For this to work, I'd have to be able to call a method by a string name however. So I researched if I can do something like that. And this is what I found:
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheCommandString);
theMethod.Invoke(this, userParameters);
ottobar's response here
I know that certain langues are not SUPPOSED to do certain things.
Like C# can use var or dynamic, but it is best to avoid them as much as possible.
My understanding is this is because C# (unlike things like Python) like to work with "knowns", and doesn't like "unknowns"
Too Long Didn't Read:
Is Type thisType = this.GetType(); something I should only use in very specific situations like var and dynamic?
You should only use that (...MethodInfo theMethod = thisType.GetMethod(TheCommandString)
when you don't have access to the method (TheCommandString) or the class , so it mostly can be used in building something like DI , or in libraries like newtonsoft or automapper.
so in your example if you have access to your method (TheCommandString) you should directly call that ( object.TheCommandString(userParameters)) , if you don't have access (eg : its a private method inside a dll) then you can use reflection.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I create a new DataContract and I need to add a new field.
That field will be describe a type of export job: Ad-hoc or virtual standby.
Which type do I need to choose to represent this idea: bool or enum? Why?
Consider how it will be used as method parameter
void F(DataContract dataContract)
If it's Enum, you invoke it via
F(DataContract.AdHoc)
F(DataContract.VirtualStandby)
They are all very clear. What if it's bool:
F(bool dataContract)
Then invoke it via
F(true)
Execute me! Is it Ad Hoc or Virtually Standy? You have educate every method consumer what true/false mean in this context. Even they want to write clear code by
F(true/*Ad Hoc*/);
It's really less readable.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Assuming we have a function:
public IEnumerable<object> GetViewModel(int id) {
var records = LoadRecordById(id);
var result = (from record in records
select new {
field1 = record.data1,
field2 = record.data2,
....
}).ToArray();
return result;
}
We can return it like IEnumerable<object> and like IEnumerable<dynamic>. The question is: what to use and why? what advantages/disadvantages ?
You have an anonymous type right now.
If you cast it to object, you will have no way to get the fields out. You cannot cast it back to an anonymous object (since you don't know what it was) and you can't access the properties from object.
If you cast it to dynamic, you can access the fields as you normally would, but you will lose all type safety. It will determine the type at compile time, which means you have access to the types generated at compile time, but if you type something wrong you will get a compilation error at runtime.
Best thing to do is to create a class that has the properties you need and return a list of that class. Then you will get the properties, and will get the benefits of static typing.
You should use an interface or a base class. But to answer your question:
dynamic foo = "Some string";
foo.ToUpper(); // this works fine
foo.Something(); // compiles, but runtime error
object bar = "Some string";
bar.ToUpper(); // does not compile
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I keep seeing the following type of syntax:(string[])myList.ToArray(typeof(string));
What does it mean when the object type is declared at the front of the object in brackets, before calling a method on it?
I am struggling to locate explanations becuase I don't know what this setup would be called.
Any help appreciated.
Thanks
it's Called a Casting, Casting is usually a matter of telling the compiler that although it only knows that a value is of some general type, you know it's actually of a more specific type. For example:
object x="any string";
string s=(string)x;
if we are using the upper one then it may possible that it will through the exception at runtime like if you are using
object x="string";
int s=(int)x;
it will through the Exception at runtime unable to cast
but if you use as oprator then it will return a null rather then throwing an exception.
object x = new object();
string y = x as string; // Now y is null because x isn't a string