Archive for the ‘Programming’ Category
Posted by dariosantarelli on June 22, 2008
Here you can find an interesting diagram of programming languages history. Years go by, but surprisely you can see how apparently incompatible paths (OO and functional programming) are slowly fusing in time. For about 50 years, computer programmers have been writing code. New technologies continue to emerge, develop, and mature. Now there are more than 2,500 documented programming languages!
Here a preview

Moreover, O’Reilly has produced a poster called History of Programming Languages which plots over 50 programming languages on a multi-layered, color-coded timeline.
Posted in Programming | Leave a Comment »
Posted by dariosantarelli on March 14, 2008
The patterns & practices Enterprise Library 3.1 is a library of application blocks published for Visual Studio 2005 and designed to assist developers with common enterprise development challenges. Application blocks are a type of guidance, provided as source code that can be used “as is,” extended, or modified by developers to use on enterprise development projects. This release of Enterprise Library includes application blocks for Caching, Cryptography, Data Access, Exception Handling, Logging, Policy Injection, Security and Validation.
In this post you can find how to to get the integrated tool to work in Visual Studio 2008. Simply you have to run a registry script that will change the keys where VS looks to load the integrated tool package. After you run the script, you will need to run devenv /setup from the Visual Studio 2008 command prompt. You can download the file from the EntLibContrib project on CodePlex.
P.S.: If you already have the Enterprise Library 3.0 installed, you must uninstall it before installing the Enterprise Library 3.1.
Posted in .NET, Microsoft Technology, Programming | Leave a Comment »
Posted by dariosantarelli on March 1, 2008
LINQ’s query operators are implemented in .NET Framework 3.5. And here lies a difficulty: you might be unsuccessful in demanding that all your customers install Framework 3.5 right away. So what does this mean if you want to code in C# 3.0 and write LINQ queries?
You can use LINQBridge, a reimplementation of all the standard query operators in Framework 3.5’s Enumerable class by Joseph Albahari.
- LINQBridge is designed to work with the C# 3.0 compiler, as used by Visual Studio 2008.
- LINQBridge comprises a “LINQ to Objects” API for running local queries. (It doesn’t include an implementation of LINQ to SQL, nor LINQ to XML).
- LINQBridge also includes Framework 3.5’s generic Func and Action delegates, as well as ExtensionAttribute, allowing you to use C# 3.0’s extension methods in Framework 2.0. In fact
- LINQBridge lets you use nearly all of the features in C# 3.0 with Framework 2.0—including extension methods, lambda functions and query comprehensions. The only feature it does not support is compiling lambdas to expression trees (i.e., Expression<TDelegate>).
Posted in .NET, C#, Microsoft Technology, Programming | Leave a Comment »
Posted by dariosantarelli on November 21, 2007
A very useful feature included in VS2008 concerns the possibility of embedding a manifest resource which specifies UAC options in applications running on Windows Vista. First, you can create a manifest file by adding an “Application Manifest File” Item to your project (default name: app.manifest), then you can set it through the Application Tab in the Project Properties.
If you want to change the Windows User Account Control level in your manifest file, all you need is to set the value of the level attribute of the requestedExecutionLevel node with one of the following:
- asInvoker (default): the application will run using the current Windows user provileges
- requireAdministrator: the application requires an Administrator user
- highestAvailable: highest privileges for the current user will be used
Posted in .NET, Microsoft Technology, Programming | 4 Comments »
Posted by dariosantarelli on November 8, 2007
If you are looking for a smart way to encrypt/decrypt sensitive data in memory in order to make them unreadable even in malicious memory dumps, maybe you need to use the ProtectedMemory class (namespace System.Security.Cryptography). This class is a managed wrapper included in the DPAPI (Data Protection API) and it works in the same way as ProtectedData (a similar class indeed used to persist encrypted strings in file/database): it exposes two static methods, Protect and Unprotect, which allow you to encrypt/decrypt strings in memory. The only requirement you have to consider in order to avoid a CryptographicException is about using 16-bytes blocks for computing encrypted/decrypted data. For example, the following code shows how to protect/unprotect a sensitive string in memory during a process execution:
Here the output:
Before Protect: xxxxxxxxxxxxxxxx
After Protect: 7j??‼8y_??PQ<?¶-
After Unprotect: xxxxxxxxxxxxxxxx
Some further considerations:
- The Protect()/Unprotect() method works directly on the original data, not on a copy. So, we don’t need to explicitly destroy our sensitive strings after the use.
- Many people use SecureString, a more intuitive class using ProtectedMemory to automatically encrypt strings. A SecureString can be modified till the MakeReadOnly() method call and can be programmatically disposed. Finally, its MemoryProtectionScope is SameProcess: a SecureString can’t be used in CrossProcess and SameLogon scenarios, in which there’re multiple applications running in different processes (CrossProcess) and allowing data encryption/decryption to the same Windows user (in particular cases, consider the impersonation technique).
- I’ve noticed some strange behaviours by randomly analyzing the memory dump file after invoking the Protect() method on a string: sometimes the text I’d like to protect appears as clear text…mmmm…. Does Windows operate a swap on file system before data encryption? If this is a possibility, are my efforts unuseful?
Posted in .NET, C#, Programming | 1 Comment »
Posted by dariosantarelli on September 14, 2007
To retrieve the original logical DB Name from a Backup file (es. .bak), you just have to call the RestoreFileList(…) method from a Restore class instance, naturally after adding a file Device. This method returns a DataTable containing a row for each added device. So, each datarow contains some metadata, including the original logical DB name, which can be found on the first column:
using System.Data;
using Microsoft.SqlServer.Management.Smo;
…
Server SQLServerInstance = new Server(“MyServer\\MyInstance”);
Restore restore = new Restore();
restore.Devices.AddDevice(“BackupFile.bak”, DeviceType.File);
DataTable RestoreInfoDT = restore.ReadFileList(SQLServerInstance);
DataRow drow = RestoreInfoDT.Rows[0];
restore.Database = RestoreInfoDT.Rows[0][0].ToString(); // Retrieves the original logical DB name
restore.SqlRestore(SQLServerInstance);
Posted in .NET, C#, Microsoft Technology | 1 Comment »
Posted by dariosantarelli on September 10, 2007
I’d like to advice this page (MSDN) where you can find the unified C# Language Specification which covers all the features of the C# language through version 3.0 (just 500 pages!!!)… In this document there are a lot of interesting and not so broadly known features (e.g. atomic operations on integral types like ulong). I think just a quick look will be fine in order to discover some curiosities
.
P.S.: To people thinking C# as a not standard language, please read the Introduction:
… C# is standardized by ECMA International as the ECMA-334 standard and by ISO/IEC as the ISO/IEC 23270 standard. Microsoft’s C# compiler for the .NET Framework is a conforming implementation of both of these standards…
Posted in .NET, C#, Microsoft Technology | 1 Comment »
Posted by dariosantarelli on September 5, 2007
If you want to retrieve a DataRow from a DataGridViewRow (for example starting from a DataGridViewSelectedRowCollection), you simply need to get the Row property of the DataRowView obtained by converting the DataBoundItem object of the DataGridViewRow :
DataGridViewSelectedRowCollection rows = MyDataGridView.SelectedRows;
foreach (DataGridViewRow row in rows)
{
DataRow myRow = (row.DataBoundItem as DataRowView).Row;
// do something with your DataRow…
}
Hope it helps
Posted in .NET, C#, Programming | 12 Comments »
Posted by dariosantarelli on August 6, 2007
An interesting feature of the .NET Framework 3.5 is that any set of methods available on an instance of a particular type is open to extension. Effectively, adding new methods to existing types is now possible.By using extension methods, we can declare static methods that can be invoked using the instance method syntax. In order to declare an extension method we must specify the keyword this as the first parameter of the static method.
Let’s show an example:
1. We have a Customer class, which needs of another method “Logout”:
2. Instead of using an inherited class, we add the “Logout” method to our Customer class by declaring a static method which can be invoked directly in any Customer class instance.

3. Finally, in our code we can invoke our “Logout” extension method in this way:

When extension methods are consumed, the argument that was declared with the keyword this is not passed, while all other arguments will be passed and seen by intellisense. So, the first argument in the declaration (keyword this) allows the compiler to determine the class instances on which the extension method is called.
Remember that extension methods, though declared as static, can be called only on instances values!!!
Posted in .NET, C#, Programming | 12 Comments »
Posted by dariosantarelli on July 26, 2007
If you want to set a Custom Property (like OpenRowSet) of a Data Flow Component (like an OLE DB Source) in a Data Flow Task you can use the code below:
using Microsoft.SqlServer.Dts.Runtime;
// Classes and interfaces to create Data Flow Components and automate Data Flow Tasks
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
…
TaskHost MyTaskHost = (TaskHost)package.Executables[0]; // Pay attention to the ordinal number
// MainPipe: wrapper to configure a Data Flow Task programmatically
MainPipe dataFlowTask = MyTaskHost.InnerObject as MainPipe;
// Each Data Flow Task component is represented by a ComponentMetadata
IDTSComponentMetaData90 MyComponentMetadata = dataFlowTask.ComponentMetaDataCollection["MyComponentName"];
// Access to the Custom Property “OpenRowset”
IDTSCustomProperty90 CustomProperty = MyComponentMetadata.CustomPropertyCollection["OpenRowset"];
CustomProperty.Value = “MyValue”;
…
Posted in .NET, C# | 1 Comment »
Posted by dariosantarelli on July 20, 2007
In C# 3.0, creating an object instance and initializing it is now possible by using the new ”object initializers” . Here an example:
class Contact
{
public string Name;
public readonly Location location = new Location();
}
...
Contact myContact = new Contact
{
Name = "Dario",
Location = { Address="myAddress", City = "MyCity" }
};
P.S.: This feature is NOT available in VB9 because it doesn’t support read-only properties/fields initialization.
Posted in .NET, C# | Leave a Comment »
Posted by dariosantarelli on May 3, 2007
The SharpOS project is aimed at writing an operating system in 100% C# with a strong sense of security and managability . Currently under construction…
Posted in .NET, C# | 2 Comments »
Posted by dariosantarelli on April 20, 2007
In the code below you can find a way to use the MOSS 2007 Administration Object Model to programmatically create a search scope in your Shared Service Provider and include your content sources to the created search scope.
Hope it helps…
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;
…
SearchContext context;
using (SPSite site = new SPSite(“http://<SSPsite>”))
{
context = SearchContext.GetContext(site);
}
Schema sspSchema = new Schema(context);
ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
Scopes scopes = new Scopes(context);
ScopeCollection sspScopes = scopes.AllScopes;
Scope newScope = sspScopes.Create(“Name”, “Description”,
null, // System.Uri object representing the owning site URL
true, // True to display the scope in the SSP Administrator UI
null, // A string specifying the alternate results page for the scope
ScopeCompilationType.AlwaysCompile); // Includes your content sources to the created Scope
foreach (string ContentSourceName in YourContentSourcesNames)
{
newScope.Rules.CreatePropertyQueryRule(ScopeRuleFilterBehavior.Include,
properties["ContentSource"], // Managed Property
ContentSourceName);
}
// Update Scopes
scopes.StartCompilation();
…
Posted in .NET, C#, Microsoft Technology, Programming | 1 Comment »
Posted by dariosantarelli on April 3, 2007
The LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities. In other words, LINQ (“Language Integrated Query”) allows developers to query in-memory data inline.
If you aren’t familiar with this language, try this link: 101 LINQ Samples (why 101 and not 100???)
As a developer, I’ve always thought that sample code is the best way to learn a technology
.
In addition, there are different versions of LINQ: for database queries and XML queries we have, DLINQ and XLINQ (files .doc from Microsoft) respectively.
Posted in .NET, C#, Microsoft Technology, Programming | Leave a Comment »
Posted by dariosantarelli on March 12, 2007
Reflector is the class browser, explorer, analyzer and documentation viewer for .NET. Reflector allows to easily view, navigate, search, decompile and analyze .NET assemblies in C#, Visual Basic and IL. The new version (5.0.7.0) also supports .NET 3.5 and query expression reverse engineering. Moreover, you can find a lot of Add-Ins here (Codeplex).

Posted in .NET, Programming | Leave a Comment »