Archive for the ‘C# 3.0’ Category
Posted by jpluimers on 2012/07/12
Didn’t need it until now, as now I wrote my first unit test on an internal class, with the unit test in a separate assembly.
Visual Studio 2010 suggested adding the InternalsVisibleTo Attribute to the assembly containing the internal class specifying that the unit test assembly would have access to it.
For me that felt up-side-down, but thinking again it is logical, but still doesn’t feel well.
This is what it does:
The InternalVisibleToAttribute was added in .Net 2.0 and most people seem to be using it in order expose internal methods to external unit test classes. However, there is nothing to prevent you from using it in non-testing situations., although I have not seen a good reason other then unit testing to use it.
–jeroen
via: C# InternalVisibleTo Attribute | Salvo(z).
Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, Prism, Software Development, VB.NET | Leave a Comment »
Posted by jpluimers on 2012/07/05
Recently I was asked to investigate a performance problem with a certain .NET application.
The first error I got when getting the app to build in Visual Studio 2010, and then run it was like this:
System.ComponentModel.InvalidAsynchronousStateException was caught
Message=An error occurred invoking the method. The destination thread no longer exists.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.WaitForWaitHandle(WaitHandle waitHandle)
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at UI.Splash.SetStatus(String status) in C:\...\Splash.cs:line 395
at UI.Menu.Main() in C:\...\Menu.cs:line 4275
InnerException:
Someone built their own splash logic with multi-threading.
Funny that today, this got answered on StackOverflow by [WayBack] mgie: [WayBack] multithreading – TMonitor synchronization / Application.ProcessMessages – Stack Overflow.
Though that is a Delphi link (and points to the nice libraries [Archive.is] AsynCalls and [WayBack] OmniThreadLibrary), the most important link it contains is to [WayBack] Borland Newsgroup Archive :: borland.public.delphi.internet.winsock :: Re: Disconnect TIdHttp in thread.
That sounds like a Delphi link too, but the subtitle “‘Ways to get into avoidable trouble with threads, V1.2′” hints the essence: it is a post that describes in an environment-agnostic way how to avoid multi-threading problems.
Recommended reading!
Anyway: Building multi-threaded code is hard. Even harder fleshing out all the corner cases and potential error conditions.
No matter what kind of programming environment: If you have not done lots of multi-threaded programming, then please don’t do it yourself: go ask someone that does know how to do it. Or better, try to avoid it.
I try to let libraries to the handling of multi-threading for me, if I use multi-threading at all, as others are far better at this than I am.
–jeroen
Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Conference Topics, Conferences, Delphi, Development, Event, Java, Software Development, VB.NET, VBS, Visual Studio 2010, Visual Studio and tools, WinForms | 6 Comments »
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 »
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 »
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 »
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 »
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:
- Mahin Gupta | Winforms splash screen – Great work by Tom. which is an update of
- 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 »
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 »
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:
- Right click your .edmx file
- Choose “Open with”
- Choose the “XML (text) editor”
- Find the ProviderManifestToken attribute
- Change the value (usually from “2008”) into “2000”
- Save the .edmx file
- 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 »
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 »