The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

Archive for the ‘.NET 4.0’ Category

.NET/C# assemblies and namespaces

Posted by jpluimers on 2013/07/23

Ever since I started .NET programming after .NET Beta 1 Arrived in 2001, I found that many people struggle with the relation between assemblies and namespaces.

So I was glad that I posted this answer about 2.5 years ago on StackOverflow. Below is the slightly edited form:

People are easily confused by the namespace/assembly thing, as it decouples the concept of where your code is physically located (the assembly) and how you reference it:

  • logically reference is by using the namespace
  • physical reference is by referencing the assembly

I usually explain the relation using the word contribute:

  1. An assembly can contribute to multiple namespaces.
    For instance, the System.Data.dll assembly contributes to namespaces like System.Data (e.g. the class System.Data.DataTable) and Microsoft.SqlServer.Server (e.g. the class Microsoft.SqlServer.Server.SqlContext).
  2. Multiple assemblies can contribute to a single namespace.
    For instance both the System.Data.dll assembly and the System.Xml.dll assembly contribute to the System.Xml namespace.
    Which means that if you use the System.Xml.XmlDataDocument class from your project, you need to reference the System.Data.dll assembly.
    And if you use the System.Xml.XmlDocument class, you need to reference the System.Xml.dll from your project.

(the above examples are .NET 4.0, but likely hold for previous .NET versions as well).

Danny Thorpe explained the concept of namespace and internal really well, so I won’t go into detail about those.

Ever since I started .NET courses 10 years ago, I draw a table explaining assemblies and namespaces like this:

Assemblies contributing to namespaces
Assembly Namespaces it contributes to
System.Data Microsoft.SQLServer.Server System.Xml
↑ Example classes
System.Data.dll DataTable SqlContext XmlDataDocument
System.Xml.dll XmlDocument

–jeroen

via: C# assemblies, whats in an assembly? – Stack Overflow.

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, .NET CF, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

Igor Ostrovsky: C# – The C# Memory Model in Theory and Practice;

Posted by jpluimers on 2013/07/16

Just found out that Igor Ostrovsky wrote two really nice articles on .NET memory management as part of his great series of other .NET articles there:

  1. C# – The C# Memory Model in Theory and Practice.
  2. C# – The C# Memory Model in Theory and Practice, Part 2.

–jeroen

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Development, Software Development | Leave a Comment »

Using ILDASM to determine the .NET Framework version an Assembly/DLL requires (via StackOverflow)

Posted by jpluimers on 2013/07/11

Josh Stodola wrote a nice answer on the Determine .NET Framework version for dll – Stack Overflow question for using ILDASM to show the required .NET Framework/CLR version for an assembly.

From that, I wrote this tiny batch file:

ildasm.exe %1 /metadata[=MDHEADER] /text /noil | find "Metadata section:"

It gives output like this:

ildasm.exe C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.XML.dll /metadata[=MDHEADER] /text /noil | find "Metadata section:"
// Metadata section: 0x424a5342, version: 1.1, extra: 0, version len: 12, version: v4.0.30319

The cool thing is that older ILDASM versions work on assemblies requiring newer .NET Frameworks/CLRs.

So it is relatively future proof.

–jeroen

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

.NET/log4net configuration: LevelMatchFilter with DenyAllFilter to discard several log levels within a range

Posted by jpluimers on 2013/06/27

log4net configuration can be a tad intimidating at first.

When you do not want to log all levels, and the levels you want to log are not noncontinuous, then you can combine the LevelMatchFilter with a DenyAllFilter.

The filter classes in the log4net do not provide much help on this, but the filters section in the log4net manual is better.

This StackOverflow question has a very nice answer explaining it: Discarding several log levels within a range with log4net.

–jeroen

via: Discarding several log levels within a range with log4net – Stack Overflow.

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

Embedded Interop Assemblies (via: CLR and DLR and BCL, oh my! – Whirlwind Tour around .NET 4) – Scott Hanselman

Posted by jpluimers on 2013/06/26

Need to put some research in this, as I have the idea that under some circumstances embedding Office interop assemblies not always works.

–jeroen

via: CLR and DLR and BCL, oh my! – Whirlwind Tour around .NET 4 (and Visual Studio 2010) Beta 1 – Scott Hanselman.

Posted in .NET, .NET 4.0, .NET 4.5, Development, Office PIA, Software Development | Leave a Comment »

Samples Environment for Microsoft Chart Controls – Release: Samples for Chart Control – .NET Framework 4

Posted by jpluimers on 2013/06/19

I hadn’t done Charting for a while, but these got going very quickly:

Samples Environment for Microsoft Chart Controls – Release: Samples for Chart Control – .NET Framework 4.

There are example downloads for ASP.NET and WinForms requiring .NET 4 or higher.

In addition, these starting points also proved to be really helpful:

Since my objective was adding charts to an existing WinForms business app, this was the namespace at hand: System.Windows.Forms.DataVisualization.Charting Namespace ().

If you are at .NET 3.5, then start at DataVisualization.Charting.Chart simple example – C# – Snipplr Social Snippet Repository.

–jeroen

Posted in .NET, .NET 4.0, .NET 4.5, ASP.NET, C#, C# 3.0, C# 4.0, Development, Software Development, VB.NET, Visual Studio 2010, WinForms | Leave a Comment »

C#/.NET/LINQ: DotNetStat: netstat.exe with TCP ports grouped by host

Posted by jpluimers on 2013/06/18

A lot of people have written .NET equivalents of netstat code. Basically there are two starting points:

I adapted the first, made the output very much like the built-in Windows netstat, and added some LINQ code to demonstrate grouping and ordering.

Now you get grouped output like this:

Distinct Remote Address:Port pairs by Remote Address:
107.20.249.140   443
107.20.249.78    443
127.0.0.1        6421, 19872
192.168.1.81     17500, 61678
199.47.218.159   443
199.47.219.148   80
199.47.219.160   443
23.21.220.140    443
23.23.127.94     443

The code below is part of the DotNetStat example.

It demonstrates a few important LINQ aspects beyond the LINQ Query Expressions (C# Programming Guide) intro and 101 LINQ Samples in C#.:

Read the rest of this entry »

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Jon Skeet, Software Development | Leave a Comment »

.NET/C# – CreateTemporaryRandomDirectory (via: Stack Overflow)

Posted by jpluimers on 2013/06/13

A while ago I needed unique temporary directories. This appears not to be standard functionality in the .NET System.IO.Path or System.IO.Directory class.

Josh Kelley asked a question about it before, and I adapted the example by Scott-Dorman based on GetTempPath and GetRandomFileName  and comments by Chris into a more robust CreateTemporaryRandomDirectory one that works in .NET 2.0 and higher:

using System.IO;

namespace BeSharp.IO
{
    public class DirectoryHelper
    {
        public static string GetTemporaryDirectory()
        {
            do
            {
                try
                {
                    string tempPath = Path.GetTempPath();
                    string randomFileName = Path.GetRandomFileName();
                    string tempDirectory = Path.Combine(tempPath, randomFileName);
                    Directory.CreateDirectory(tempDirectory);
                    return tempDirectory;
                }
                catch (IOException /* ex */)
                {
                    // continue
                }
            } while (true);
        }
    }
}

You can call it like this: Read the rest of this entry »

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | 2 Comments »

.NET/C#: from Unicode to ASCII (yes, this is one-way): converting Diacritics to “regular” ASCII characters.

Posted by jpluimers on 2013/06/11

A while ago, I needed to export pure ASCII text from a .NET app.

An important step there is to convert the diacritics to “normal” ASCII characters. That turned out to be enough for this case.

This is the code I used which is based on Extension Methods and this trick from Blair Conrad:

The approach uses String.Normalize to split the input string into constituent glyphs (basically separating the “base” characters from the diacritics) and then scans the result and retains only the base characters. It’s just a little complicated, but really you’re looking at a complicated problem.

Example code:

using System;
using System.Text;
using System.Globalization;

namespace StringToAsciiConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string unicode = "áìôüç";
            string ascii = unicode.ToAscii();
            Console.WriteLine("Unicode\t{0}", unicode);
            Console.WriteLine("ASCII\t{0}", ascii);
        }
    }

    public static class StringExtensions
    {
        public static string ToAscii(this string value)
        {
            return RemoveDiacritics(value);
        }

        // http://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net
        private static string RemoveDiacritics(this string value)
        {
            string valueFormD = value.Normalize(NormalizationForm.FormD);
            StringBuilder stringBuilder = new StringBuilder();

            foreach (System.Char item in valueFormD)
            {
                UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(item);
                if (unicodeCategory != UnicodeCategory.NonSpacingMark)
                {
                    stringBuilder.Append(item);
                }
            }

            return (stringBuilder.ToString().Normalize(NormalizationForm.FormC));
        }
    }
}

–jeroen

Posted in .NET, .NET 3.5, .NET 4.0, .NET 4.5, ASCII, C#, C# 3.0, C# 4.0, C# 5.0, Development, Encoding, Software Development, Unicode | Leave a Comment »

Some links on Registry and LINQ

Posted by jpluimers on 2013/05/28

A few notes for my research list:

One important thing that most of the code examples miss is to close the registry keys when they are done with them.

 

–jeroen

Posted in .NET, .NET 4.0, .NET 4.5, C#, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »