background preloader

C#

Facebook Twitter

C# covariant return types utilizing generics. Write Mobile Agents In .NET To Roam And Interact On Your Network. Wandering Code Write Mobile Agents In .NET To Roam And Interact On Your Network Matt Neely Code download available at:MobileAgents.exe(145 KB) Recently I had the opportunity to return to school to complete a graduate degree. This experience made me realize two important things: there are some cool ideas in academia that seem to never find the light of day in the professional setting, and the academic world at large is not yet very familiar with the Microsoft® .NET Framework.

The term agent originates in artificial intelligence and describes a logical entity that has some level of autonomy within its environment or host. In this introduction to mobile agents, I'll create an example mobile agent system and develop several mobile agent applications, highlighting some of the problems that I encountered along the way as well as several ideas for overcoming them. Introduction to Mobile Agents Figure 1 Traveling Agent Hops Between Machines Figure 2 Task Agent Allocates Tasks Assembly Resolution.

Output an Assembly Version/Fully Qualified Name from the CommandLine. Sample of non-CAS custom permission with declarative form supported. - Eugene Bobukh's WebLog. Why? Recently, I started seeing numerous requests regarding creation of custom permissions that do not inherit from CodeAccessPermission and thus do not perform stackwalk. There is nothing special about implementing such classes. In fact, it is easier then with CodeAccessPermission as a base. However, having a sample handy, I just decided to share it here along with my comments. So welcome WorkingTimePermission Trying to be at least somewhat close to real life, I implemented the permission object with the following Demand semantics: If Demand is performed during business hours, it passes.

If it is done in other time, it throws the SecurityException. As you see, it’s straightforward. [method:WorkingTimePermissionAttribute(SecurityAction.Demand)] private static void AccessibleDuringBusinessHoursOnly() …instead of performing time checks explicitly on each protected function entrance. 1. Using System; using System.Security; using System.Security.Permissions; namespace CustomPermissions Common = 0x00, Demystifying C# 3.0 - Part 7: Expression Trees. In this series of Demystifying C# 3.0 we have already covered - a) Demystifying C# 3.0 - Part 1: Implicitly Typed Local Variables "var"b) Demystifying C# 3.0 - Part 2: Anonymous Typesc) Demystifying C# 3.0 - Part 3: Extension Methodsd) Demystifying C# 3.0 - Part 4: Lambda Expressionse) Demystifying C# 3.0 - Part 5: Object and Collection Initializersf) Demystifying C# 3.0 - Part 6: (LINQ) Query Expression Translation (to C# 3.0) Thank you to both Eric Wise, and C.

Steen for linking. The more people we have look at these posts, the better the overall discussion quality will be. Okay, so next we will be talking about "Expression Trees". You must read about Lambda Expressions before you read this post. So in short, Expression trees permit lambda expressions to be represented as data structures, instead of executable code. X => x + 1, is executable code. which could also be written as, Func<int,int> f = x => x + 1 But, Expression<Func<int,int>> e = x => x + 1 ; This covers the basics of C# 3.0. Demystifying C# 3.0 - Part 6: (LINQ) Query Expression Translation (to C# 3.0) In this series of Demystifying C# 3.0 we have already covered - a) Demystifying C# 3.0 - Part 1: Implicitly Typed Local Variables "var"b) Demystifying C# 3.0 - Part 2: Anonymous Typesc) Demystifying C# 3.0 - Part 3: Extension Methodsd) Demystifying C# 3.0 - Part 4: Lambda Expressionse) Demystifying C# 3.0 - Part 5: Object and Collection Initializers I *strongly* recommend reading up the above in sequence before reading this post.

This is, (I feel) a rather good post, that will set LINQ in your mind clearly. If you rush through this post, you will waste this opportunity. If you are crystal clear about the above 5 posts and the concepts behind them, .. read on .. Okay good, so in this post, we are going to talk about "Queries" or "LINQ" for the very first time in this Demystifying C# series. The practical application of the above 5 C# 3.0 features (which are not the complete set of new things in C# 3.0), can best be understood by understanding Query Expression Translation. WHOAA !!! Cool huh? Demystifying C# 3.0 - Part 4: Lambda Expressions. In the Demystifying C# 3.0, I've already talked about - a) Demystifying C# 3.0 - Part 1: Implicitly Typed Local Variables "var"b) Demystifying C# 3.0 - Part 2: Anonymous Typesc) Demystifying C# 3.0 - Part 3: Extension Methods Lets next talk about Lambda Expressions.

The best way I can describe Lambda Expressions are - C# Anonymous Methods, only a lot cooler. So what is an anonymous method in C# 2.0? Well y'know you can write code like below in C# 1.x/2.x - class SomeClass{ delegate void SomeDelegate(); public void InvokeMethod() { SomeDelegate del = new SomeDelegate(SomeMethod); del(); } void SomeMethod() { Console.WriteLine("Hello"); }} class SomeClass{ delegate void SomeDelegate(); public void InvokeMethod() { SomeDelegate del = delegate() { Console.WriteLine("Hello"); }; del(); }} Wunner'ful.

Lambda expressions give you an even more concise, functional syntax using the"=>" token. In general, the syntax is parameters => expression Lets talk about these one by one - But wait a minute. Finally - Demystifying C# 3.0 - Part 3: Extension Methods. If you've been following my blog, you would have noticed that I've been trying to talk about new C# 3.0 features one by one. I am trying to take the technical jargon out, and bring these features down to an understandable level. You may want to read the two features I have already talked about below - a) Demystifying C# 3.0 - Part 1: Implicitly Typed Local Variables "var"b) Demystifying C# 3.0 - Part 2: Anonymous Types So here goes, Part 3: Extension methods. Frequently you are handed a class, such as System.String, or System.Int32, that you "wished" had that one extra method that would make your life so much easier - but damn it doesn't have that method. Extension methods, solve that problem.

So if you wanted an extra method called "SpankMonkey", you could now write code like below int i = 10 ;i.SpankMonkey() ; // Spanks the monkey per the logic you wrote. How neat !!! So how do you write an extension method? Sure you could have complicated implementations such as - Whoaa that's confusing. :) Demystifying C# 3.0 - Part 2: Anonymous Types. In my last blog entry, I had talked about Implicitly typed local variables and the "var" keyword.

If you have stumbled across this post through a search engine, or otherwise, I would recommend reading that first. Assuming that you understand "var" - lets dive into part 2 of Demystifying C#3.0 ~ Anonymous Types. In C# 2.0, lets say you wanted to represent a person, you would typically have to write a class as follows - public class Person{ string hairColor ; string skinColor ; int teethCount ;} // I'm obviously taking the shortcut by not creating properties .. anyway, that's besides the point. Well, in LINQ, I could be querying arbitrary groups of my data. Enter anonymous types. Var monster = new {hair="black", skin="green", teethCount=64} ; The above line of code will work even if you do not declare a class called "Person" in advance. The above line of code simply generates a class under the scenes (you never see it), that looks like as below - Demystifying C# 3.0 - Part 1: Implicitly Typed Local Variables "var"

I am going to publish a series of blogposts that intend to bring C# 3.0 down to earth. C# 3.0, along with LINQ, and all the heavy duty talk that surrounds it has sort of made it difficult to understand IMO. Well - if not difficult to understand, it sure is tough to gauge - "Where to begin". So with 5 minutes a day, a short post a day will dice and slice a single feature, and we will logically move to a fuller picture of C# 3.0, followed by LINQ, and DLINQ (if I still have steam left). So here we go, with the first in that series, the "var" keyword. In C# 2.0, you can declare an integer (or anything for that matter of fact) as - int i; You could also write something like - int i = 1; Generally speaking - <datatype><variablename> = <initializer> ;Okay good. Var i = 1; But what is "var"? But then what is the difference between "var", "object" and "variant" (from COM or VB6 days).

Variants were oink oink piggies, basically a catch all pig that occupied way too much memory. Great !!! Cutting Edge: Customize Your Open File Dialog. Cutting Edge Customize Your Open File Dialog Dino Esposito Code download available at:CuttingEdge0303.exe(96 KB) Displaying an Open File dialog is certainly easy in the Microsoft® .NET Framework with Windows® Forms, but the resulting window is not as customizable as when you create it through the Win32® API. With Windows 2000, Microsoft added a nice feature—the places bar, which is the vertical toolbar that appears on the left side of the window to let you select a frequently visited folder. As you can see in Figure 1, the places bar contains buttons to take the user directly to five folders—History, Desktop, My Documents, My Computer, and My Network Places.

Figure 1 Selecting a Folder in the Places Bar When coding against the Open File common dialog in the Win32 API, you can set a style to hide the places bar. In this column, I'll focus on the places bar of the Open File common dialog. OpenFileDialog Notice the special formatting required by the Filter property string.

Figure 4 Two Places. Creating Your Own Code Access Permissions. There are other situations in which a custom permission might be appropriate. When a built-in code access permission class protects a resource but does not sufficiently control access to that resource, you might need a custom code access permission. For example, an application might use personnel records for which each employee record is stored in a separate file; in such a case, read and write access could be controlled independently for different types of employee data.

An internal management tool could be authorized to read certain sections of an employee's personnel file but not to modify those sections. In fact, it might not even be allowed to read some sections. Wherever possible, permissions should not overlap. Having more than one permission protecting a resource presents a significant problem for administrators, who must then be sure to deal appropriately with all the overlapping permissions every time they configure the rights to access that resource. Reference Concepts. Class Data Binding using Custom Attributes. Download source files - 1.73 KB Introduction At the end of this article, you should be comfortable using Custom Attributes. This article focuses on using them in order to create a quick, flexible data binding mechanism. Prerequisite Experience/Knowledge At least a beginner's understanding of ADO.NET and Reflection is required to follow this article.

Understanding Attributes Attributes are used to add additional metadata to any element within a class (including the class itself). [<Attribute>(<attribute values>)] <target element> The syntax for defining an attribute is to place the following System attribute before the class that will define your custom: [AttributeUsage(AttributeTargets. The Attribute itself can target a class, constructor, field, method, property or all (meaning the attribute could be applied to anything, but you need to be careful with this depending on what you'll be using the attribute to indicate).

Reading Custom Attributes All Together Now... History. Why are NameValueCollection lookups slower than Hashtable? [Kim Hamilton] - BCL Team Blog. An internal discussion came up recently on the performance difference of lookups in Hashtable versus NameValueCollection. Benchmarks revealed that NVC lookups were ~2-8 times slower than Hashtable. For example, when doing 40,000 lookups on a collection size of 100,000, NameValueCollection is about 2.6x worse: This leads to the question of whether NameValueCollection and Hashtable have different asymptotic behavior or if it simply has additional overhead.

By repeating the benchmark over increasing collection sizes, it’s clear that the asymptotic behavior is the same for lookups. This confirms what we expected – that lookup cost is constant for both. If there are multiple entries associated with the key, it will return them appended together. NameValueCollection c = new NameValueCollection(); c.Add("dogs", "Bubba"); c.Add("dogs", "Mojo"); Console.WriteLine(c["dogs"]); // returns Bubba,Mojo Let’s move on to some other interesting performance aspects of NameValueCollection.

.NET Matters: ICustomTypeDescriptor, Part 1. .NET Matters ICustomTypeDescriptor, Part 1 Stephen Toub Code download available at:NETMatters0504.exe(163 KB) Q I write a lot of one-off utilities for personal use, and since they don't require any sophisticated user interfaces, I often use a System.Windows.Forms.PropertyGrid bound to a settings class in order to allow the user to configure a tool's operations. Unfortunately, sometimes I don't write these settings classes, and often they've been constructed in a way that's incompatible with the PropertyGrid.

For example, I often bind to client proxy classes created by wsdl.exe in order to make Web service requests from my tool. The problem is that these proxy classes expose fields rather than properties. A It sounds like you're using the Microsoft® .NET Framework 1.x, given your description of wsdl.exe and the proxy classes it generates. One such type, System.ComponentModel.TypeDescriptor, is used to retrieve information about the properties and events a particular component exposes.

An MSN Messenger Log Listener. Download demo project and source - 296 Kb The code download has been updated for the final release of the Enterprise Library, including design support for the EntLibConfig application. Introduction A group of programmers, far smarter than I, have been laboring from some time to create a library of utilities to help developers like myself to create better applications.

The fruits of that labor is the Enterprise Library from the Patterns & Practices group at Microsoft. The Enterprise Library encompasses Exception Handling, Logging, Caching, Security, Cryptography, and Database Access. The focus of this article is the Logging bits from the Enterprise Library. The good stuff I used the FormattedDatabaseTraceListener as a reference for creating my listener. The actual listener class, responsible for sending log messages as MSN Messenger messages.

Our listener class is the FormattedMsnMessengerTraceListener which I will cover in depth. FormattedMsnMessengerTraceListener ctor if (String.Empty ! Avoiding configuration pitfalls with incompatible copies of Enterprise Library - Tom Hollander's blog. When you install Enterprise Library 3.0, you actually get two distinct copies of the library. One copy is in the form of pre-compiled binaries - by default these get installed to "C:\Program Files\Microsoft Enteprise Library 3.0 - April 2007\bin".

The other copy is in the form of source code, which by default will be compiled and the assemblies coped to "C:\EntLib3Src\App Blocks\bin". While both copies of Enterprise Library contain identical code, there is one critical difference: the pre-compiled binaries are strong-named (with a Microsoft key that we do not ship), and the assemblies compiled from the source code are not initially strong-named. So as far as .NET is concerned, these two sets of assemblies are completely different and completely incompatible with one another. The good news is that all of these problems can be avoided easily enough if you keep a few things in mind.

Parallel Performance: Optimize Managed Code For Multi-Core Machines. Design: Task Parallel Library explored - MSDN Utopia. Enable/Disable Network Connection - David Aiken. Large Data and Streaming. Using Custom Filters in the Enterprise Library Logging Block. Script# Gemrcsharpcs.pdf (application/pdf Object) The most complete C# Webbrowser wrapper control.

C# - How can I get a value of a property from an anonymous type. Flag enum confusion C# Menus, part III : animated multi-level drop-down menu. Continuation-Passing Style Simplifies Your C# Exception Handling Code « Paint.NET Blog. Exploring Lambda Expression in C# HTTP Communication and Security with Silverlight. .NET Matters: Abortable Thread Pool. Coding Best Practices Using DateTime in the .NET Framework. Popup Window Finder and Mouse Tracker in C# Low-Level Mouse Hook in C# - Stephen Toub. Blog Archive » DynamicObject in C# 4.0. Pattern Matching in C# - Part 1.