0%

My new favorite C# language feature: CallerMemberName

This is how a property with INotifyPropertyChanged support looks like pre C# 4.5:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public string OldStylePropertyChanged
{
get { return _oldStyle; }
set
{
if (value != _oldStyle)
{
_oldStyle = value;
OnPropertyChangedOldStyle("OldStylePropertyChanged");
}
}
}
private void OnPropertyChangedOldStyle(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

And this is how it will look like when we apply the use of CallerMemberName in our PropertyChanged method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public string DynamicTitle
{
get { return _dynamicTitle; }
set
{
if (value != _dynamicTitle)
{
_dynamicTitle = value;
OnPropertyChanged();
}
}
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

Some more really cool additions to CallerMemberName is the following Parameter attributes:

  • [CallerMemberName]
  • [CallerFilePath]
  • [CallerLineNumber]

The name of the attributes is straight forward and easy to understand, so I doubt that I would have to describe them in detail, instead, let’s look at an example straight from MSDN on how to use them.

1
2
3
4
5
6
7
8
9
10
11
12
13
public sealed class Logger
{
public void TraceMessage(string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Debug.WriteLine("message: " + message);
Debug.WriteLine("member name: " + memberName);
Debug.WriteLine("source file path: " + sourceFilePath);
Debug.WriteLine("source line number: " + sourceLineNumber);
}
}

Happy hacking!

VS 2008, COM interop, and Microsoft.VisualStudio.Shell.Interop.dll

When I try to compile our VsPackages with VS 2008 the compilation fails. The compilation fails with the message:

The assembly ‘Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’
is not registered for COM Interop. Please register it with regasm.exe /tlb.

This is a known bug that was discovered right as VS2008 was going out the door. Microsoft have a fix in place (to be delivered with VS 2008 SP1)

https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=10671

Visual Studio .NET MVC EF Released
VS2002 1.0 N/A N/A January 2002
VS2003 1.1 N/A N/A April 2003
VS2005 2.0 N/A N/A November 2005
VS2008 2.0,3.0,3.5 N/A N/A November 2007
VS2008SP1 2.0,3.0,3.5SP1 Add-in 1.0 Yes August 2008
VS2010SP1 2.0,3.0,3.5,4.0 2.0,3.0 Yes April 2010
VS2010SP1 2.0,3.0,3.5SP1,4.0 2.0,3.0 Yes March 2011
VS2012 2.0,3.0,3.5,4.0,4.5 3.0,4.0 Yes August 2012
VS2012SP2 2.0,3.0,3.5,4.0,4.5 3.0,4.0 Yes April 2013
VS2012SP3 2.0,3.0,3.5,4.0,4.5 3.0,4.0 Yes June 2013
VS2013 2.0,3.0,3.5,4.0,4.5.1 4.0 Yes October 2013
VS2015 2.0,3.0,3.5,4.0,4.5.1,4.5.2,4.6 Yes

VS2010SP1: When you have SP1 installed you’ll see instead: Microsoft Visual Studio 2010 Version 10.0.40219.1 SP1Rel Microsoft .NET Framework Version 4.0.30319 SP1Rel

VS2012SP2: Add Fakes Framework that is the next generation of Moles & Stubs. (http://research.microsoft.com/en-us/projects/moles/) Fakes is different from Moles, however, so moving from Moles to Fakes will require some modifications to your code. The Moles framework will not be supported in Visual Studio 2012.

C# .NET CLR MVC Razor Not support OS
C# 1.0 1.0 1.0 N/A N/A
C# 1.2 1.1 1.1 N/A N/A
C# 2.0 2.0 2.0 MVC1 N/A
C# 3.0 3.5 2.0 MVC2 N/A
C# 4.0 4.0 4.0 MVC3,4 Razor
C# 5.0 4.5 4.0 MVC4 XP,2003
4.5.1 4.0
4.5.2 4.0

C# 1.2 Features: First version to call Dispose on IEnumerators which implemented IDisposable

C# 2.0 Features: Generics, anonymous methods, nullable types

C# 3.0 Features: Lambda expressions, extension methods, expression trees, implicit type(var), query expressions

C# 4.0 Features: Late binding(dynamic), named arguments and optional

C# 5.0 Features: Async programming, caller info attributes

VS.NET Path
VS2008 “C:\Program Files\Microsoft Visual Studio 9.0”
VS2010 “C:\Program Files\Microsoft Visual Studio 10.0”
VS2012 “C:\Program Files\Microsoft Visual Studio 11.0”
VS2013 “C:\Program Files\Microsoft Visual Studio 12.0”

There are some known issues using EF 4.x in a .NET 4.5 project.
We recommend installing a pre-release version of EF 5, which is designed to work with .NET 4.5

Update: 2015.03.15

I ran into an interesting error message this morning while run application.

—— Build started: Project: UnitTests, Configuration: Debug Any CPU ——
Consider app.config remapping of assembly “yourAssembly, Culture=neutral, PublicKeyToken=xxx”
from Version “2.2.0.0” [] to Version “2.2.8.0”
[C:\Demo\yourAssembly.dll] to solve conflict and get rid of warning.
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets :
warning MSB3247: Found conflicts between different versions of the same dependent assembly.

This occurs because YourApp depends on a different version of the your Assembly.

Fortunately, the error message gives you all you need to know to fix the problem. I added an App.config file to the project with the following XML.

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly" publicKeyToken="96D09A1EB7F44A77" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.2.8.0" newVersion="2.2.8.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

How to find public key token for a .NET DLL or assembly?

Use the following command,

sn -T myDLL.dll

This will give you the public key token. Remember one thing this only works if the assembly has to be strongly signed.

Example

1
2
3
4
5
C:\WINNT\Microsoft.NET\Framework\v3.5>sn -T myDll.dll
Microsoft (R) .NET Framework Strong Name Utility Version 3.5.21022.8
Copyright (c) Microsoft Corporation. All rights reserved.

Public key token is x77x5x561934x089

  1. Press ALT + SPACE to bring up the system menu (you won’t see it because it is off screen)
  2. Press M to select the “Move” menu choice.
  3. Press one of the arrow keys to initiate the movement.

Now just use the mouse to place the window where you want.

In fact, as long as we remember a few shortcuts, you can easily solve this small problem:

「Shift」+「Win」+ Left/Right Arrow: You can work around the screen to quickly switch windows.

「Alt」+ Space +「M」: Can move the cursor quickly “stick” to the currently displayed warning window, when you move the mouse, you can drag the window to the front of the visible screen.

We can redirect to a specific version of a .net assembly by using configuration file as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly"
publicKeyToken="32ab4ba45e0a69a1"
culture="en-us" />
<!-- Assembly versions can be redirected in application,
publisher policy, or machine configuration files. -->
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Running VMware Workstation 6.5 (or above 6.5). My Bridged networking stops working after Windows sleeps or hibernates. A restart is required to restore its operation. NAT networking works fine.
Anyone have any ideas how to restore it without a reboot?

Please run the following two commands in a cmd as administrator:

1
2
net stop vmnetbridge
net start vmnetbridge

I’m using System.Data.OracleClient.OracleCommand to create a table and fill it out with some data. The query I am using runs OK in PS/SQL Developer, however when I’m trying to execute it from within .NET application I’m getting this error:

ORA-06550: line 1, column 20:
PLS-00103: Encountered the symbol “” when expecting one of the following:

begin function package pragma procedure subtype type use form current cursor

Here is some code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var text = @"declare cnt number;
begin

select count(*) into cnt from all_tables
where table_name = 'TABLE_A';


if cnt = 1 then
begin
execute immediate 'truncate table TABLE_A';
execute immediate 'drop table TABLE_A';
end;
end if;

execute immediate 'create table TABLE_A as
(SELECT DISTINCT v.ID, g.ext_id FROM VIEW_A v
JOIN TABLE_B B ON v.id = B.Id
WHERE YEAR1 = ''2008'')';

end;");
cmd.CommandText = text;
cmd.ExecuteNonQuery();

Search Google, finally found:
I think you need to lose just the ‘\r’ characters.

1
2
3
text = text.Replace("\r\n", "\n");
cmd.CommandText = text;
cmd.ExecuteNonQuery();

Basically, add more “Collate SQL_Latin1_General_CP1_CI_AS” in after the conditions, it should be followed would be a case-sensitive.

1
2
3
4
SELECT *
FROM dbo.CaseSensitiveTest
WHERE Value1 LIKE '%Test%' Collate SQL_Latin1_General_CP1_CI_AS
GO

VirtualBox often used to test the system, but the most troublesome is that VirtualBox to use uuid to control vdi file, so there is no way to use copy commands to copy vdi file.

Therefore, if you want to copy a vdi file, you must use the following command to re-generate a new uuid.

VBoxManage clonevdi Orig.vdi New.vdi

If you copy the exhausted only to find, there are still commands can be remedied.

VBoxManage internalcommands setvdiuuid New.vdi