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 ‘C# 5.0’ Category

It works! Install Visual Studio 2010 AFTER Visual Studio 2012 – Stack Overflow

Posted by jpluimers on 2013/08/06

Never thought this would work so smoothly: Install Visual Studio 2010 AFTER Visual Studio 2012 – Stack Overflow.

–jeroen

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, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 8.0, VB.NET 9.0, Visual Studio 11, Visual Studio 2010, Visual Studio and tools | 7 Comments »

.NET/C# – InvalidCastException for two Objects of the same type (via: G+/Stack Overflow)

Posted by jpluimers on 2013/08/03

If you ever get something similar to

InvalidCastException: [A]Person cannot be cast to [B]Person.

then you are (or more precise: your process is) probably loading the same assembly twice, but from a different context.

.NET allows that, and for reason (side by side loading, appdomains, etc).

Delphi doesn’t (thanks Warren Postma for noticing). But a mistake you see quite often is that Delphi developers try to load the same Delphi type from both an EXE and a DLL wondering why they don’t match. If you want to spread your types in Delphi, then use BPLs (run-time packages) for that. BPLs are similar to .NET assemblies, but cannot be loaded in memory twice.

–jeroen

via c# – InvalidCastException for two Objects of the same type – 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, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 8.0, VB.NET 9.0, Visual Studio 11, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »

Reminder to Self: Messagepack / BSON / ProtocolBuffers

Posted by jpluimers on 2013/07/25

Need to find/create a Delphi compatible version of Messagepack or BSON.

–jeroen

via Serializing and deserializing (packing/unpacking) to a file and/or memorystream with MessagePack in C# ».

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, Delphi, Delphi XE2, Delphi XE3, Development, Software Development | 4 Comments »

.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 »

C#: combining “adding `char` and `int` and “`a += b` means `a = a + b`, but `a += b + c` does not mean `a = a + b + c`”.

Posted by jpluimers on 2013/07/18

A while ago, I wrote about .NET/C# duh moment of the day: “A char can be implicitly converted to ushort, int, uint, long, ulong, float, double, or decimal (not the other way around; implicit != implicit)”.

There is another duh moment having to do with the various C# operators like += which is being described as being

a += b

is equivalent to

a = a + b

You might think that this also holds:

a += b + c

is equivalent to

a = (a + b) + c

But Eric Lippert has explained this is not the case: it is equivalent to:

a = a + (b + c)

In his explanation, he also shows the confusion can get you very surprising results if you mix string, chars and ints in the expression: depending on the statement and ordering, you either concatenate characters, or add ints to characters.

He also recommends you should not do concatenation: either use String.Format, or StringBuilder. I totally agree with that.

Recommended reading!

–jeroen

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, 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 »

“Fragile Base Class problem” C# posts by Eric Lippert

Posted by jpluimers on 2013/07/04

I’m not the only a big fan of  almost anything that Eric Lippert writes on tech subjects on both his new personal Fabulous Adventures In Coding blog as well as his old microsoft Fabulous Adventures In Coding blog (a little more than half a year ago, he moved from Microsoft to Coverity.

On his old blog, he had a great series of posts on the “Fragile Base Class problem” (which he calls the Brittle Base Class problem) from a C# perspective.

Be sure to also read the comment threads, as they provide some very valuable information too (for instance, about Extension Methods that can make your code more fragile).

Recommended reading!

–jeroen

via: Browse by Tags – Fabulous Adventures In Coding – Site Home – MSDN Blogs.

Posted in .NET, 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 »

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 »