.NET 4.0 application to use Microsoft ADO Ext. 6.0 for DDL and Security
I try to compile Xsd2Db solution in .NET 4.0 framework. I add a reference to "Microsoft ADO Ext. 6.0 for DDL and Security" and use it in this way -
using System;
using System.IO;
using ADOX;
namespace Xsd2Db.Data
{
/// <summary>
/// Summary description for JetDataSchemaAdapter.
/// </summary>
public sealed class JetDataSchemaAdapter : AdoxDataSchemaAdapter
{
/// <summary>
///
/// </summary>
internal const string Extension = ".mdb";
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
internal static string GetPath(string name)
{
return Path.GetFullPath(
Path.ChangeExtension(name, Extension));
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
internal static string GetConnectionString(string name)
{
return String.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Engine Type=5;",
GetPath(name));
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
protected override void DeleteCatalog(string name)
{
File.Delete(GetPath(name));
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
protected override void CreateCatalog(string name)
{
// Force the run-time to let go of the file! Otherwise,
// cleanup and other operations might fail because the file
// will still be in use.
Catalog catalog = new CatalogClass();
catalog.Create(GetConnectionString(name));
catalog.ActiveConnection = null;
catalog = null;
GC.Collect();
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
protected override Catalog OpenCatalog(string name)
{
Catalog catalog = new CatalogClass();
catalog.let_ActiveConnection(GetConnectionString(name));
return catalog;
}
}
}
Now I have an error during compiling -- "The type 'ADOX.CatalogClass' has no constructors defined".
Clicking the error link brings me to the meta data -
#region Assembly Interop.ADOX.dll, v4.0.30319
// C:\xsd\xsd2db\xsd2db\Common\DataSchemaAdapte开发者_高级运维r\obj\Debug\Interop.ADOX.dll
#endregion
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace ADOX {
[Guid("00000602-0000-0010-8000-00AA006D2EA4")]
[ClassInterface(0)]
[TypeLibType(2)]
public class CatalogClass : _Catalog, Catalog {
public CatalogClass();
[DispId(1)]
public virtual dynamic ActiveConnection { get; set; }
[DispId(4)]
public virtual Groups Groups { get; }
[DispId(2)]
public virtual Procedures Procedures { get; }
[DispId(0)]
public virtual Tables Tables { get; }
[DispId(5)]
public virtual Users Users { get; }
[DispId(3)]
public virtual Views Views { get; }
[DispId(6)]
public virtual dynamic Create(string ConnectString);
[DispId(7)]
public virtual string GetObjectOwner(string ObjectName, ObjectTypeEnum ObjectType, object ObjectTypeId = Type.Missing);
[DispId(1)]
public virtual void let_ActiveConnection(object pVal);
[DispId(8)]
public virtual void SetObjectOwner(string ObjectName, ObjectTypeEnum ObjectType, string UserName, object ObjectTypeId = Type.Missing);
}
}
We can see public CatalogClass(); but not a constructor.
I believe in .NET 1.x this worked. But why .NET 4.0 has this problem? Anyone encoutered this and What is the best way to handle this?
Thanks!
I did a search and found out the problem does exist in .NET 4.0. Here are some explanations and solutions -
http://blogs.msdn.com/b/mshneer/archive/2009/12/07/interop-type-xxx-cannot-be-embedded-use-the-applicable-interface-instead.aspx;
http://mokosh.co.uk/post/2010/04/10/net-4-0-interop-type-cannot-be-embedded-use-the-applicable-interface-instead/
精彩评论