.NET Questions and Solutions

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘Type’

BUG: You receive a “Type mismatch” error message when you assign a value type variable to a property through COM InterOp in Visual Basic .NET or in Visual Basic 2005

Symptoms
When you assign a value to a property of a Component Object Model (COM) object in .NET, you may receive the following error message when you run your application:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in InterOpDemo.exe
Additional information: Type mismatch
Resolution
The problem occurs if all of the following conditions are true: You are using the COM object in early bound mode.The property in the COM object has both Set and Let methods.You are trying to pass a value type variable to that property. In early bound mode, Microsoft Visual Basic .NET and Visual Basic 2005 always call the Set method of the property if it is available. If you want to call the Let method, you must explicitly specify it.

BUG: PInvoke method with System.GUID return type raises exception error

Symptoms
When you run Platform Invocation Services (PInvoke) with System.GUID as the return type, the process is unsuccessful and you receive the following exception error message:

An unhandled exception of type ‘System.Runtime.InteropServices.MarshalDirectiveException’ occurred in TestPInvoke.exe Additional information: Method’s type signature is not PInvoke compatible.
Resolution
Microsoft has confirmed that this is a problem in Visual Studio .NET.

BUG: Error message when you try to pass a Collection object from Visual Basic 6.0 components to Visual Basic 2005 or to Visual Basic .NET: “System.InvalidCastException”

Symptoms
When you try to pass a Collection object fromMicrosoft Visual Basic 6.0 components to Microsoft Visual Basic 2005 or to Microsoft Visual Basic .NET, you may receive an error message. In Microsoft Visual Studio 2005, you receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ApplicationName.exe
Additional information: Unable to cast object of type ‘Microsoft.VisualBasic.Collection’ to type ‘VBA.Collection’.In Microsoft Visual Studio .NET, you receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ApplicationName.exe
Additional information: Specified cast is not valid.If you examine the type of the collection object that Visual Basic 2005 or Visual Basic .NET expects, you find that Visual Basic 2005 or Visual Basic .NET expects the VBA.Collection type instead of the Microsoft.VisualBasic.Collection type. If you change your code to pass a collection object of the VBA.Collection type, you receive the following error message on the line of code where you try to create a new instance of the VBA.Collection class:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in ApplicationName.exe
Additional information: COM object with CLSID {A4C4671C-499F-101B-BB78-00AA00383CBB} is either not valid or not registered.This problem also occurs in other Microsoft .NET Framework-supported languages such as Microsoft Visual C# 2005 and earlier versions of .NET Framework-supported Microsoft Visual C#.
Resolution
The InvalidCastException error occurs because the Microsoft.VisualBasic.Collection type is incompatible with the VBA.Collection type. The COMException error occurs because only a Visual Basic 6.0 application can create an instance of the VBA.Collection class. You cannot create an instance of the VBA.Collection class outside a Visual Basic 6.0 application.

BUG: “A reference to ‘<typeLibrary>’ could not be added” error message when you try to add a project reference to a COM DLL

Symptoms
When you try to add a project reference to a Component Object Model (COM) DLL in Microsoft Visual Studio .NET, you may receive an error message that is similar to the following:

A reference to ‘C:\MyCOMDLL\Debug\MyCOMDLL.dll’ could not be added. Converting the type library to a .NET assembly failed. Could not load type MyCOMDLLLib.MyClassClass from assembly Interop.MyCOMDLLLib, Version=1.0.0.0.
Additionally, when you try to convert the type definitions that are found in a COM DLL by using Microsoft Type Library Importer (Tlbimp.exe) from a command prompt, you may receive an error message that is similar to the following:

TlbImp error: System.TypeLoadException – Could not load type MyCOMDLLLib.MyClassClass from assembly MyCOMDLLLib, Version=1.0.0.0.
Resolution
You may notice the behavior that is mentioned in the “Symptoms” section when the following conditions are true: Your DLL contains a class (such as MyClass) that implements an interface (such as IMyClass) that in turn derives from a base interface (such asIBaseClass).The IMyClass interface contains a method (such as Test) that has the same name as a property that the IBaseClass interface has.You notice this behavior because of the mechanism that Tlbimp.exe uses to disambiguate member names. Under the previous conditions, when the Microsoft .NET Framework tries to create a method implementation to associate the Test method of the MyClass class with the corresponding interface method, the .NET Framework does not know the name of the corresponding interface method. Therefore, Tlbimp.exe generates a System.TypeLoadException error, and then you receive the error message that is mentioned in the “Symptoms” section.
When you try to add a project reference to a COM DLL, Visual Studio .NET internally runs Tlbimp.exe and then handles any generated exceptions. Therefore, under the previous conditions, Visual Studio .NET handles the generated System.TypeLoadException, and then you receive the error message that is mentioned in the “Symptoms” section.

Binary serialization exceptions occur when you serialize or deserialize the DateTimeFormatInfo class or the TextInfo class between different versions of the .NET Framework

Symptoms
When you serialize or deserialize the DateTimeFormatInfo class or the TextInfo class between a computer that is running the Microsoft .NET Framework 2.0 Service Pack 1 (SP1) and a computer that is running a pre-SP1 version of the .NET Framework 2.0, you receive one of the following binary serialization exceptions:
Exception 1

Exception type: System.Runtime.Serialization.Serialization
ExceptionMessage: Binary stream ‘0′ does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.Exception 2

Exception type: System.ArgumentNull
ExceptionMessage: Value cannot be null.This problem occurs when you serializethe DateTimeFormatInfo class and the TextInfo class by setting the TypeFormat property of the BinaryFormatter class to FormatterTypeStyle.XsdString.
Resolution
This problem occurs because the FormatterTypeStyle.XsdString serialization formatting and the FormatterTypeStyle.TypesWhenNeeded serialization formatting drop type information for performance reasons when you serialize types across different versions of the .NET Framework.

An exception error is generated after you run a Set-AttachmentfilterListConfig command together with the ExceptionConnectors option on an Exchange 2007 SP1-based server

Symptoms
When you run the Set-AttachmentfilterListConfig command together with the -ExceptionConnectors option on an Exchange Server 2007-based server, the command doesnot run successfully. Additionally, an exception is generated.
For example, you may run the following command:
Set-AttachmentfilterListConfig -ExceptionConnectors d18f518a-38e3-4349-9697-bc2ac318461f When you run this command, you receive the following error message:

WARNING: An unexpected error has occurred and debug information is being generated: Unable to cast object of type ‘System.Collections.Generic.List`1[Microsoft.Exchange.Data.Directory.SystemConfiguration.ReceiveConnector]‘ to type ‘Microsoft.Exchange.Data.Directory.SystemConfiguration.ReceiveConnector[]‘. Set-AttachmentfilterListConfig : Unable to cast object of type ‘System.Collections.Generic.List`1[Microsoft.Exchange.Data.Directory.SystemConfiguration.ReceiveConnector]‘ to type ‘Microsoft.Exchange.Data.Directory.SystemConfiguration.ReceiveConnector[]‘. At line:1 char:31 + Set-AttachmentfilterListConfig? <<<< -ExceptionConnectors d18f518a-38e3-4349-9697-bc2ac318461f Additionally, the follow event is logged in the Application log:
Event Type:?Error
Event Source:?MSExchange Common
Event Category:?General
Event ID:?4999
User:??N/A
Computer:?Servername
Description: Watson report about to be sent to dw20.exe for process id: <ID>, with parameters: E12, c-RTL-x86, <version>, powershell, M.E.Management, M.E.M.M.A.SetAttachmentFilterListConfig.InternalProcessRecord, System.InvalidCastException, 4e90, <version>.? ErrorReportingEnabled: False For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
Resolution
To resolve this problem, install Update Rollup 2 for Exchange Server 2007 Service Pack 1. For more information about Update Rollup 2 for Exchange Server 2007 Service Pack 1, see the following Exchange Help topic:
Description of Update Rollup 2 for Exchange Server 2007 Service Pack 1 (http://go.microsoft.com/fwlink/?LinkId=152558)For more information about how to install the latest Exchange service pack or update rollup, see the following Exchange Help topic:
How to Obtain the Latest Service Pack or Update Rollup for Exchange 2007(http://go.microsoft.com/fwlink/?LinkId=152570)