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# 4.0’ Category

.NET/C#: setting private fields and properties through reflection (via: .net – Is it possible to set private property via reflection – Stack Overflow)

Posted by jpluimers on 2012/04/19

The Stack Overflow question “Can I set a private property via reflection?” was answered “Yes, it is” with some good examples on how to set private fields and properties through reflection.

Recommended reading!

–jeroen

via .net – Is it possible to set private property via reflection – Stack Overflow.

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

.NET/C#: Way overdue post – the syntax inside the Obsolete attribute; Looking for the new ConfigurationManager? (via: @ScottCate Blog Moved ….)

Posted by jpluimers on 2012/04/18

Everytime I get a warning like

Warning 1 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by ConfigurationManager.AppSettings'

it reminds me I should have written a blog post about it, as the solution is a tiny bit more than just replacing System.Configuration.ConfigurationSettings.AppSettings by System.Configuration.ConfigurationManager.AppSettings.

Scott Gate wrote a nice post on his old blog about this (his new blog is awesome, he really should import his old posts into his new blog) that explains how to solve this well, so below is an elaboration on the how of the change, a tiny trick and a short series of steps to resolve this warning.

First of all, the above message means you are touching code that has been written in the .NET 1.x era, and the maintaining people (you! <g>) have been too lazy to solve the warning. That is bad, as your code should compile without warnings, and preferably without hints too. Read the rest of this entry »

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

Getting the public static readonly strings and public const strings (and their values) from a class

Posted by jpluimers on 2012/04/03

Quite a few projects have one or more classes with with a bunch of public const string or public static readonly string values. Use const when things are really constant (like registry configuration keys), use static readonly when – upon change – I do not want to recompile dependent assemblies. Many people recommend static readonly over const.

Having members in stead of string literals scattered all over the place allows you to do compile timing checking, which I’m a big fan of as in the age of things getting more and more dynamic, you need to have as many sanity checks as possible.

One of the checks is to verify these const members and their values. Sometimes you need the list of members, sometimes the list of values, and sometimes each member value should be the same as the member name.

The listing below shows the code I came up with.

It works with code like this, and those can have more code and other fields (non string, non public) in them as well:

namespace bo.Literals
{
    public class FooBarPublicStringConstants
    {
         public const string Foo = "Foo";
         public const string Bar = "Bar";
    }
    public class FooBarPublicStaticReadOnlyStringFields
    {
         public static readonly string Foo = "Foo";
         public static readonly string Bar = "Bar";
    }
}

I started out with this code, but that is limited to classes only having public const fields. Not flexible enough. Read the rest of this entry »

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

naming – What’s the use/meaning of the @ character in variable names in C#? – Stack Overflow

Posted by jpluimers on 2012/03/28

Duh, I always thought @ could only be used for strings.

Not so: just like with the & in Delphi is used to escape keywords, the additional use of @ in C# is to escape identifiers:

The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier.

–jeroen

via: naming – What’s the use/meaning of the @ character in variable names in C#? – Stack Overflow.

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

reflection – C# – Resolving a parameter name at runtime – Stack Overflow

Posted by jpluimers on 2012/03/27

So I won’t forget: a GetName method returning the name of a parameter, local or field.

Tags: C#, reflection, IL parsing, argument name, anonymous type, generic type cachegeneric type caching.

–jeroen

via: reflection – C# – Resolving a parameter name at runtime – Stack Overflow.

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, Software Development | 4 Comments »

.NET/C#: Resolved errors “The type ‘NameSpace.TypeNameClass’ has no constructors defined” and “Interop type ‘NameSpace.TypeNameClass’ cannot be embedded. Use the applicable interface instead.” (via Stack Overflow: Interop type cannot be embedded)

Posted by jpluimers on 2012/03/20

When moving the Microsoft Scripting Runtime interop code from .NET 1.x to 4.x, I got these errors:

Error1: The type 'Scripting.FileSystemObjectClass' has no constructors defined
Error21: Interop type 'Scripting.FileSystemObjectClass' cannot be embedded. Use the applicable interface instead.

Though the first answers on the question seem to adequately resolve the problem, they merely cure the symptom: turning off the embedding of the PIA (Primary Interop Assembly).

The below answer by Michael Gustus (which only has a few votes, so please vote it up) actually explains what is going on, and solves the cause:

In most cases this error is the result of code which tries to instantiate a COM object e.g. here piece of code starting up Excel:

Excel.ApplicationClass xlapp = new Excel.ApplicationClass();

Typically, in .Net 4 you just need to remove the ‘Class’ suffix and compile the code:

Excel.Application xlapp = new Excel.Application();

MSDN explanation here.

Hence I like the comment by Tyrsius on this answer as well:

This was more useful than the marked answer, as I needed the functionality of the embedded Interop. This solved both problems, thank you!

The above answer tells you to not use the class type, but the interface type, just like the error states. And the answer implicitly tells you the class type is ApplicatoinClass, and the interface is Application.

These are the declarations for the PIA interface: Read the rest of this entry »

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

WTF C# code of the month

Posted by jpluimers on 2012/03/14

When I come across code like this, I’m always astonished:

			catch(Exception ex)
			{
				string strMess = ex.Message;
			}

What was the person thinking when he wrote this? Did he get distracted and nobody else notice this before checking it into their version control system?

In the same module:

		private string ToString(object myVal)
		{
			try
			{
				if (myVal != System.DBNull.Value)
					return myVal.ToString().Trim();
			}
			catch{}

			return "";
		}

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

Eric Lippert’s comment and answer explaining nullable operator lifting (c# – Why does the == operator work for Nullable when == is not defined? – Stack Overflow)

Posted by jpluimers on 2012/03/07

It seems I’m not the ony one who watches what Eric Lippert writes closely.

Eric works at the C# team at Microsoft (since 1996, which is about the time Anders Hejlsberg joined Microsoft).

Unlike Anders, Eric is much more visible. I regularly read his blog, and watch his StackOverflow.com contributions (RSS feed) on a regular base.

Recently, he posted a awesome comment “Nullable is nothing but magic” on a the question “C# – Why does the == operator work for Nullable when == is not defined?“, together with a very concise answer explaining that in C# most operators are ‘lifted to nullable’.

Note his tiny – but important – mention that for == VB.net behaves different than C#.

Note that Eric is very productive, he usually contributes to StackOverflow.com multiple times a day, sometimes with material that (at least for me <g>) need a while before I really get the point.

Recommended reading :)

–jeroen

via: c# – Why does the == operator work for Nullable when == is not defined? – Stack Overflow.

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

Anyone with a C#, Delphi or FreePascal implementation of the PRESENT Ultra-Lightweight Block Cipher encryption?

Posted by jpluimers on 2012/03/06

A short while ago a paper got published on PRESENT: An Ultra-Lightweight Block Cipher by Andrey Bogdanov et al becoming ISO standard 29192-2:2012.

Is there anyone that has a C#, Delphi or FreePascal implementation with unit tests?

–jeroen

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Delphi, Development, Software Development | 13 Comments »

Free .NET Decompiler – JustDecompile from Telerik

Posted by jpluimers on 2012/02/17

Interesting:

JustDecompile is a new, free developer productivity tool for easy .NET assembly browsing and decompiling.

–jeroen

via: Free .NET Decompiler – JustDecompile.

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