| Thomas 的个人资料Blogs日志列表 | 帮助 |
|
9月27日 Compiling and running a dynamic method at runtimeThis issue might be very useful, for instance if you need to perform an expression for each item in a list, that is depending on the user's input. instead of writing a static method, that is handling all possibilites with conditions like "if (flag_set) {}", it's possible to dynamically create a small C# source code that is optimized for that special purpose, compile it and simply use it whilst enumerating the list. the following link shows the way: http://www.codeproject.com/cs/algorithms/matheval.asp 9月22日 Compression and DecompressionFor compressing/decompressing purposes of bytes, .NET 2.0 offers functionality for it. You can either use the DeflateStream class or the GZipStream class, both available in the System.IO.Compression assembly. See the MSDN for further information. 9月14日 Example: Draw a gradiented background for a panel: private void XDTFind_OnPaintPanel(object sender, PaintEventArgs e)
if (searchPanel.ContainsFocus) } 9月11日 RemotingRemoting describes how to call methods on a server using the tcp protocoll: http://www.microsoft.com/germany/msdn/nettv/folge1.mspx#EAC Connection to a web service (the simplest way):Accessing to an existing web service in .NET is as easy as it can be: Remember the previous generated web service that has the following members
Now let the service run in the background and create a new form application. Then, In the solution explorer you select "Add new web Reference..." from the context menu. In the dialog, type in the url adress of the running web service (in this example it's "http://localhost:1122/WebSite2" and then you'll see the directory list, where you select "service.asmx" and then you finally need to click the button "Add Reference" and a new file "localhost" is added to your form project in the Web References section on the solution explorer. Now you can simply access all methods of the web services as simple as accessing to properties of an object. See a simply example here: private void button1_Click(object sender, EventArgs e) /// Call a method form the web service. That' all! So within one minute, you can access all methods of an existing web service. By the way, the web reference (in this example named localhost) provides 2 method access members for each method on the web service:
private void button1_Click(object sender, EventArgs e) /// Add an event that occures when the web service has finished to process the /// now executhe the Merge method asynchronously: /// <summary> 9月7日 Lists, arrays and collections.Sometimes you need to build a class that requires a property that holds several objects of the same type.
Intuitively, you'd choose a generic List<myobject> type for the property, but thats not the optimal choice.
Because using a List means that the list must be previousely created with several List.Add() calls. But sometimes it could be easier to create a simple ObjectType[] array like
if the objects for the list are constant and only a view.
This is much more shorter than
To offer both possibilites, simply don't specify List<> as the property type rather than ICollection<>, as both List<myobject> and myobject[] have implemented the ICollection<myObject>.
So finally we come to the conclusion:
Prefer to specify ICollection<myobject> as a property type rather than List<myobject>.
9月5日 Creating a Web Client to access a previousely created Web ServiceAfter creating our first web service, we are no prepared to access it's members using HTML Post functionality, which goes like this: private void button1_Click(object sender, EventArgs e) As you see, the two params for the Merge member are specified in a NameValueCollection class, while the HttpPost is done by calling WebClient.UploadValues which returns a byte[] value, that represents an xml string that looks like followed: <?xml version="1.0 encoding="utf-8"?><string xmlns="http://tempuri.rog/">Merged VR with Developpers!</string> Of course you'll have to to some additional stuff, but this is the simple way to access a web service in .NET. Note that the 1882 port may differ, so you might change it. Just click on the tool tray icon of the running web service to see it's current port number. Creating a Web Service in .NETCreating a simple web service within Visual Studio 2005 is done in a view minutes:
[WebService(Namespace = "http://www.tomsblog.de/", Description = "For testing purposes only")] } [WebMethod(Description="Returns a counter value as string")] [WebMethod(Description="Merges to strings to one.")] Now you know how to access the web service and how to create a web client comes with the next blog... Encryption and Decryption of stringFor some circumstances, you might need to encrypt a string and later decrypt it, using a symmetric key. This is an examply, how to easily do this with less effords: private void button1_Click(object sender, EventArgs e)
/// set the symmetric key from an xml string: rsa.FromXmlString(XmlKey); byte[] bytes = Encoding.ASCII.GetBytes(textBox2.Text); /// to display the encrypted data as a string, the BitConverter does a /// now decrypt the encrypted bytes[] and convert it into a readable string: Of course there are also other possibilties in .NET that might be more accurate for the purpose, like the class EncrytpedXml which is specialized for xml encryption, or take a look at the System.Security.Cryptography namespaces to explorer a varity of different symetric and asymetric encryption classes like DES, RAS, Rijandel and also common hashing algorithms like the commonly known MD5. |
|
|