|
Thanks leppie,
Acutally my question is not how to use Delegate.CreateDelegate, but what means extension method.
Any ideas?
regards,
George
|
|
|
|
|
|
|
Extension methods allow you to extend an existing class or struct by adding methods.
Consider this pointless example:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = 0;
i = i.ToMax();
Console.Write(i);
Console.Read();
}
}
public static class Extensions
{
public static int ToMax(this int i)
{
i = int.MaxValue;
return i;
}
}
} Now any time you use an integer instance it automatically has the ToMax() method available.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Thanks Dave,
Really good stuff. One more question about method group,
http://csharpindepth.com/ViewNote.aspx?NoteID=92
what means "an extension method as a method group using extension syntax" in the above link?
regards,
George
|
|
|
|
|
No, that's not how method group is used in the spec (at least how I remember it).
An expression like "Console.WriteLine" returns a method group. It is a group of methods with different signatures, and when invoking the method group or constructing a delegate from it, the compiler chooses the method (the overload) with the matching signature (actually "best matching" signature, as defined in the spec).
|
|
|
|
|
AH ok, so it's like overload resolution?
|
|
|
|
|
Yes, overload resolution looks up a method from a method group.
The spec wrote:
An expression is classified as one of the following:
...
• A method group, which is a set of overloaded methods resulting from a member lookup (§7.3). A method group may have an associated instance expression and an associated type argument list. When an instance method is invoked, the result of evaluating the instance expression becomes the instance represented by this (§7.5.7). A method group is only permitted in an invocation-expression (§7.5.5) or a delegate-creation-expression (§7.5.10.3). In any other context, an expression classified as a method group causes a compile-time error.
...
|
|
|
|
|
Agree, Daniel!
One more question about method group,
http://csharpindepth.com/ViewNote.aspx?NoteID=92
what means "an extension method as a method group using extension syntax" in the above link?
regards,
George
|
|
|
|
|
I think the author is making a reference to method group as 'all the available methods that can be used like an instance method'.
An extension method is a C# compiler construct and does not form part of the CLI spec.
|
|
|
|
|
Thanks leppie,
1.
You mean the point of author is all kinds of instances methods from all classes/namespace belongs to
a single method group?
2.
What means "extension syntax" in his words?
regards,
George
|
|
|
|
|
|
Thanks Daniel,
Two more comments,
1.
"No, that's not how method group is used in the spec (at least how I remember it)." -- I am losing the context.
You mean my understanding that "method group is a set of methods with the same signature" is wrong?
2.
"An expression like "Console.WriteLine" returns a method group." -- Console.WriteLine returns void. Why do you say it returns method group?
regards,
George
|
|
|
|
|
1.
George_George wrote: You mean my understanding that "method group is a set of methods with the same signature" is wrong?
Yes. A method group is a set of methods with different signatures. There are 19 different "Console.WriteLine" methods (19 overloads with different parameters).
2.
"Console.WriteLine" is an expression that returns a method group.
You can do one of two things with a method group:
a) Invoke a method from it: "Console.WriteLine()" - this chooses one of the overloads compatible with the parameters you are passing, and invokes it.
b) Assign it to a variable of a delegate type "Action a = Console.WriteLine" - this chooses one of the overloads compatible with the delegate signature, and creates a delegate from it.
So the return type of "Console.WriteLine" is a method-group. The return type of "Console.WriteLine()" is void.
You cannot declare "method-group" variables, only variables of delegate types that store only a single method from the group.
|
|
|
|
|
Thanks Daniel,
1.
Now I have understood why my previous understanding -- method group is a set of methods with the same signature. But what is the exact definition of a method group? I found it is too flexible to understand, e.g. how could I know whether an expression like Console.WriteLine returns a method group, any easy to understand and remember rules?.
2.
Console.WriteLine could have 19 overloads, wow! But for delegate we still need to specify the parameter type/number and return value type, how could you use a single delegate variable/type to point to different methods with different parameter type/number and return value type -- like Console.WriteLine?
regards,
George
|
|
|
|
|
Hello all.
Does anybody know how to query the available mail servers active on a mchine prior to calling the send method on the MailMessage class. i.e. There could be a SMTP or exchange server on the network.
Cheers.
|
|
|
|
|
Matt Fishbeck wrote: . There could be a SMTP or exchange server on the network.
SMTP is a protocol. Exchange is a product. Exchange can handle SMTP (if enabled).
|
|
|
|
|
Is there a call to query the STMP server to be used as a parameter to be passed into the constructor of SmtpClient?
|
|
|
|
|
If not supplied, the SmtpClient class will lookup the info in the app.config file. If not present there, it will use the defaults as defined in the machine.config file.
|
|
|
|
|
I just noticed the ServicePoint property, that could be helpful, never used it
|
|
|
|
|
We are using an exchange server. Do not know if it supports STMP but without supplying a server it throw an exception Will see what I can find out
|
|
|
|
|
Hello!
I'm starting to use C#, and I feel lost right now.
I've got a "somestruct.h" file with a C++ struct:
struct SOMESTRUCT
{
TCHAR Field1[100];
int Field2;
double Field3;
};
And, in a C++ class, I have a function that returns a SOMESTRUCT:
SOMESTRUCT someFunction();
Now I have a C# app which will call the C++ function. I have the C++ code in a DLL and I try to import the DLL so I can call that function:
[DllImport("myDLL.dll", CharSet = CharSet.Auto)]
static extern SOMESTRUCT someFunction();
Problem is: my C# app doesn't know the SOMESTRUCT struct. I can't import "somestruct.h" in C# and I really don't know how could I use that function.
I think maybe I could make a managed version of the struct SOMESTRUCT, but I don't know how to transform a fixed-size char array (Field1). If I transform it to a String it doesn't work properly.
I have also read that I could make a wrapper of the struct to get it work from C#, but I just cannot find an easy-to-understand, newbie-suitable, complete example of how to do it.
Please, could somebody help me?
|
|
|
|
|
verence333 wrote: Please, could somebody help me?
Learn C# first, then move onto more advanced topics like platform invoke (calling a native dll).
|
|
|
|
|
Hi, leppie.
Well, I'm trying to learn C# already. But I'm not learning it because I want, but because I'm required to get an application working. That application uses some C++ DLL's, and I *must* use them. Also, I don't have all the time in the world to learn how to do it, it should be done in a limited time, so I must learn advanced topics already.
I think I have no problem with C# language, it's very easy to understand and use, and the only things that trouble me are those advanced topics. I already know how to invoke functions from native DLL's, but only functions that return a standard type (int, long, etc.), and I can't find info about how to invoke functions that return a struct.
Thanks.
|
|
|
|
|
I suggest you lookup on Marshalling structures, that should give you the answer you are looking for, whether it will be understood is another story.
Good luck
|
|
|
|