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# – finding the attribute values for an assembly

Posted by jpluimers on 2012/07/05

For some version management features, I needed to find the attribute values of loaded assemblies. Googling around, I didn’t find many nice solutions. In fact the solutions I found didn’t work well (AssemblyName is not an Attribute!), and/or contained lots of duplicate code, or uses RegEx and other string comparisons (lesson: if you can do it with either string comparison or proper type checking, use the latter). Below is the code; here some explanation:

  • It uses a C# translation of the RetrieveLinkerTimestamp function found through Jeff Atwood (Coding Horror) which was published by Kevin Gearing and determines the linker timestamp from the PE Header. It corrects the time into your time zone. The C# version has two overloads (one with an Assembly parameter, the other with a string filePath), and the latter now contains some exception handling just in case you pass it nonsense.
  • The RunOnAssemblyAttribute generic method takes a T parameter of type Attribute, then runs the Action<T> action if that attribute is found in the Assembly assembly. It retreived the attribute using the Assembly.GetCustomAttributes method.
  • When a null parameter is passed, it gets the information from the executing assembly (if you want it to be another one, just change the code if you want it to)
    An alternative way of getting the executable name is at the very bottom: you need it when your assembly is loaded from an unmanaged application (GetEntryAssembly then returns null)
  • The constructor runs it on all known assembly related attributes in the System.Reflection namespace, calling RunOnAssemblyAttribute with an action that fills the property for each existing attribute (you wish all those attributes would have a Value property, but their property names are specific for each attribute). For non existing attributes, the default property values are used (which incidently are the default values for those attributes anyway).
  • There are a couple of casts (for instance AssemblyNameFlags) as the underlying attribute contains a constructor with that parameter (in this case AssemblyFlagsAttribute), but stores it in an integer without providing a cast property. Maybe there should be proper type checking; that is left as an exercise…
  • The constructor also fills a couple of non attribute properties – AssemblyName – LinkerTimestamp
  • The Name property is derived from the AssemblyName.Name property if it exists

Read the rest of this entry »

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

Visual Studio and Delphi: Getting a new GUID in the code editor

Posted by jpluimers on 2012/06/28

Earlier this week, I already wrote about different idioms in different IDEs.

Here is another one, again a feature I don’t use often: getting a fresh GUID in the IDE. Read the rest of this entry »

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Delphi, Development, Software Development, Visual Studio and tools | 7 Comments »

C# Remove Duplicate Lines From Text File? – Stack Overflow

Posted by jpluimers on 2012/06/27

Recently I had to do a quick removal of duplicate lines in bunch of text files.

A quick search revealed that back in 2009, John Skeet came to the rescue with a couple of examples (:

--jeroen

via: C# Remove Duplicate Lines From Text File? – Stack Overflow.

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Jon Skeet, Software Development | 1 Comment »

Repeating characters into strings in C# and Delphi (via: .net – Best way to repeat a character in C# – Stack Overflow)

Posted by jpluimers on 2012/06/26

Switching back and forth between mainly Delphi and C#, sometimes it is hard to remember which idiom works best in each environment.

Recently, I had to dupe a lot of tab characters for some Tab-Delimited interface to an archaic system.

I remembered the Delphi idiom: use the DupeString function as about Delphi explains (yes, I know: it dupes more than just characters).

In C#, these work best for me:

Small code sample of the first way (thanks CMS):

static string Tabs(int n)
{
    return new String('\t', n);
}

–jeroen

via: .net – Best way to repeat a character in C# – Stack Overflow.

Oh BTW: I have reduced my StackOverflow presence. It looks like the success of StackOverflow made them instantiate many moderators. A lot of those moderators work under the mantra “we follow the rules strictly, and favour punishment over encouragement” (some  even talk about “changing heritage“). That’s a real pity, as I see a lot of StackOverflow users get scared by the very active downvoting, question/answer closing and even deletion of material that is in essence valuable, if it were edited up a bit. Deleting content is always bad, as it increases the link rot that StackOverflow are trying to prevent in questions/answers as per their FAQ. Links are the foundation of the web.

Alas, devoting real attention to the quality of StackOverflow requires putting real energy in it, which for some of the moderators seems to be too much to ask.

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Delphi, Development, Pingback, Software Development, Stackoverflow | 5 Comments »

.NET/C# – WinForms splash screen research material

Posted by jpluimers on 2012/06/21

I’m in the midst of converting a suite of WinForms C# projects from a range of .NET versions (1.x till 3.x) to 4 totalling some million lines of code.

One of the problems is that some people hacked together some splash screen stuff using multi-threading, doing all sorts of things that was forbidden in .NET 1 (and broke in .NET 2+).

On my research list for getting this to work:

  1. Mahin Gupta | Winforms splash screen – Great work by Tom. which is an update of
  2. A Pretty Good Splash Screen in C# – CodeProject.

–jeroen

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, Development, Software Development, Visual Studio 2002, Visual Studio 2003, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools, WinForms | Leave a Comment »

.NET Enums enumerated: System.ArgumentException was unhandled by user code Message=An item with the same key has already been added.

Posted by jpluimers on 2012/06/13

All .NET languages have a cool feature: enumerated types, for instance enum in C#Enum in VB.NET, or the enum in the  Oxygene language of Delphi Prism. You can even specify the underlying integral type.

It allows you to name values, which can make your code very readable.

It also allows you to assign an integer to each of those values, which allows you to map them to existing integers in the ‘real’ world. This is both a powerful and potentially uncool feature of enums (the other uncool feature is that though you can have bitflags in Enums, most .NET languages don’t have a Set type. You need Set Extensions to do things like Set operations on Card Suit bitflags using C# extension methods).

Because of my background in the 80s in MuSimp and Turbo Pascal, I’ve done quite a bit of enums and sets in the past, hence the mentioned C# enum extension methods mentioned above. While writing this article I also found out Extending’ the Enum Class to Validate the Enum Values with the Flags Attribute that mentions quite a bit of stuff that is complementary to my code and what you will see below.

The risk of assigning integer values on C# enum is that you can assign the same integer value to multiple enum elements.

For instance, this code will fail: Read the rest of this entry »

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

The ADO.NET Entity Framework and SQL Server 2000: the ProviderManifestToken attribute and selecting it with XPath

Posted by jpluimers on 2012/05/23

Yes. Dorothy. There are people using the ADO .NET Entity Framework with SQL Server 2000  in parallel of moving towards a more modern Microsoft SQL Server version.

Entity Framework is lovely for developing data-centric applications.

By default, Visual Studio 2010 will target SQL Server 2008 as a database. That is fine, but it is kind of invisible it does: there is no property or dialog where you can change this.

What you have to change in order to have the Entity Framework send SQL Server 2000 compatible queries is to:

  1. Right click your .edmx file
  2. Choose “Open with”
  3. Choose the “XML (text) editor”
  4. Find the  ProviderManifestToken attribute
  5. Change the value (usually from “2008”) into “2000”
  6. Save the .edmx file
  7. Build and run your application

A few caveats:

Read the rest of this entry »

Posted in .NET, .NET ORM, C#, C# 2.0, C# 3.0, C# 4.0, Development, EF Entity Framework, Software Development, SQL Server, SQL Server 2000, SQL Server 2005, SQL Server 2008, SQL Server 2008 R2, SQL Server 2012 | Leave a Comment »

Adding a “Reverse Assignment” to CodeRush for Visual Studio .NET (via: Q35048 – DevExpress Support Center, Knowledge Base, Code Samples)

Posted by jpluimers on 2012/05/16

As a Delphi user, I’m missing the “Reverse Assignment” feature in the Visual Studio version of CodeRush.

Since CodeRush is very extendable (Mark is still explaining to people how the idea for that came from Delphi Packages back in the mid 90s), you can add this one yourself, as the a answer to this DevExpress support issue shows:

Q:

Is there a quick way to reverse the assignment in CodeRush?  For example:

FROM:
Field[“test1”] = edTest1.Value;
Field[“test2”] = edTest2.Value;

TO:
edTest1.Value = Field[“test1”];
edTest2.Value = Field[“test2”];

I know Delphi had this capability in its refactoring, does CodeRush for Visual Studio?

Thanks

Daniel Schipper


Read the rest of this entry »

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, Software Development, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »

Dodgy Coder: Coding tricks of game developers

Posted by jpluimers on 2012/04/26

Some very interesting tips from game development that apply juts as well to general software development.

On code health:

Now I always try to dig right down to the root cause of a bug, even if a simple, and seemingly safe, patch is available. I want my code to be healthy. If you go to the doctor and tell him “it hurts when I do this,” then you expect him to find out why it hurts, and to fix that.

Though tools like SourceMonitor can help you track your code health, the best tool is between your ears.

–jeroen

via: Dodgy Coder: Coding tricks of game developers.

Posted in .NET, Batch-Files, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Delphi, Delphi x64, Delphi XE2, Development, JavaScript/ECMAScript, PHP, PowerShell, Scripting, Software Development | 1 Comment »

SQL Server 2000 (and probably later) other reason for System.Data.SqlClient.SqlException: A severe error occurred on the current command. (via SQL Server Forums)

Posted by jpluimers on 2012/04/24

While transitioning from SQL Server 2000 to 2008, I recently had the “A severe error occurred on the current command. The results, if any, should be discarded.”  occurring on SQL Server 2000 in the form as shown at the bottom of this message.

Many of the search results point you into the area of atabase corruption, or in using NVARCAR parameters with SQL Server 2000 or SQL Server 2005 (the app didn’t use NVARCAR, nor did it use large VARCHAR parameters).

The cool thing on the SQL Server Forums – System.Data.SqlClient.SqlException: A severe error occurred on the current command post was that it summed up causes, and asked for more:

Posted – 06/17/2004 :  15:05:20

Rashid writes “Hi: Gurus I am getting these errors when I try to execute my application. According to MS knowledge base (http://support.microsoft.com/default.aspx?scid=kb;en-us;827366) these errors happen due to following resons

  1. You use a SqlClient class in a Finalize method or in a C# destructor.
  2. You do not specify an explicit SQLDbType enumeration when you create a SqlParameter object. When you do not specify an explicit SQLDbType, the Microsoft .NET Framework Data Provider for SQL Server (SqlClient) tries to select the correct SQLDbType based on the data that is passed. SqlClient is not successful.
  3. The size of the parameter that you explicitly specify in the .NET Framework code is more than the maximum size that you can use for the data type in Microsoft SQL Server.

None of these are true in my case. Are there any other reasons that can cause these problems..

There is one more: sending huge SQL Statements to your SQL Server is always a bad idea and gives this error too. Read the rest of this entry »

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Database Development, Development, Encoding, Software Development, SQL Server, SQL Server 2000, SQL Server 2008 R2, Unicode | Leave a Comment »