Skip to content

Commit

Permalink
Remove DuckTales code as dynamic COM calls are provided in .NET by de…
Browse files Browse the repository at this point in the history
…fault #283 [breaking-change]
  • Loading branch information
jozefizso committed Mar 4, 2023
1 parent bbb9f61 commit 0978c9a
Show file tree
Hide file tree
Showing 69 changed files with 12 additions and 5,688 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v2.0.0-preview

### Breaking Changes
* Remove "duck tales" implementation [#283](https://github.com/NetOfficeFw/NetOffice/issues/283)
Affected types:
* removed all types from namespace `NetOffice.DuckTales`


## v1.9.3

### Fixed
Expand Down
8 changes: 0 additions & 8 deletions Source/ADODB/Utils/ProjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ public string[] Dependencies
}
}

public bool IsDuck
{
get
{
return false;
}
}

public bool Contains(Type type)
{
if (null == _exportedTypes)
Expand Down
8 changes: 0 additions & 8 deletions Source/Access/Utils/ProjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ public string[] Dependencies
}
}

public bool IsDuck
{
get
{
return false;
}
}

public bool Contains(Type type)
{
if (null == _exportedTypes)
Expand Down
8 changes: 0 additions & 8 deletions Source/DAO/Utils/ProjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ public string[] Dependencies
}
}

public bool IsDuck
{
get
{
return false;
}
}

public bool Contains(Type type)
{
if (null == _exportedTypes)
Expand Down
8 changes: 0 additions & 8 deletions Source/Excel/Utils/ProjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ public string[] Dependencies
}
}

public bool IsDuck
{
get
{
return false;
}
}

public bool Contains(Type type)
{
if (null == _exportedTypes)
Expand Down
8 changes: 0 additions & 8 deletions Source/MSComctlLib/Utils/ProjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ public string[] Dependencies
}
}

public bool IsDuck
{
get
{
return false;
}
}

public bool Contains(Type type)
{
if (null == _exportedTypes)
Expand Down
8 changes: 0 additions & 8 deletions Source/MSDATASRC/Utils/ProjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ public string[] Dependencies
}
}

public bool IsDuck
{
get
{
return false;
}
}

public bool Contains(Type type)
{
if (null == _exportedTypes)
Expand Down
196 changes: 2 additions & 194 deletions Source/NetOffice/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Runtime.InteropServices;
using System.Collections.Generic;
using NetOffice.Loader;
using NetOffice.Duck;
using NetOffice.Exceptions;
using System.Diagnostics;

Expand Down Expand Up @@ -90,7 +89,6 @@ public partial class Core
/// </summary>
private static Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");

private Dictionary<Type, Type> _duckingCache;
private static Core _default;
private bool _initalized;
private List<ICOMObject> _globalObjectList = new List<ICOMObject>();
Expand Down Expand Up @@ -461,24 +459,6 @@ internal Assembly ThisAssembly
/// Cache as Type ID => ParentLibrary ID
/// </summary>
internal Dictionary<Guid, Guid> HostCache { get; private set; }

/// <summary>
/// Duck Type Cache
/// T1 is interface
/// T2 is its implementation
/// </summary>
private Dictionary<Type, Type> DuckingCache
{
get
{
lock (_thisAssemblyLock)
{
if (null == _duckingCache)
_duckingCache = new Dictionary<Type, Type>();
}
return _duckingCache;
}
}

/// <summary>
/// Dependent assemblies analyzed by LoadAPIFactories
Expand Down Expand Up @@ -590,27 +570,15 @@ public void ClearCaches(bool forceClear)
}

/// <summary>
/// Get wrapper class factory info as non duck
/// Get wrapper class factory info.
/// </summary>
/// <param name="caller">calling instance</param>
/// <param name="comProxy">new created proxy</param>
/// <param name="throwException">throw exception if no info found or return null</param>
/// <returns>factory info from corresponding assembly</returns>
internal IFactoryInfo GetInstanceFactoryInfo(ICOMObject caller, object comProxy, bool throwException = true)
{
return this.GetFactoryInfo(HostCache, caller, comProxy, false, throwException);
}

/// <summary>
/// Get wrapper class factory info as duck
/// </summary>
/// <param name="caller">calling instance</param>
/// <param name="comProxy">new created proxy</param>
/// <param name="throwException">throw exception if no info found or return null</param>
/// <returns>factory info from corresponding assembly</returns>
internal IFactoryInfo GetDuckFactoryInfo(ICOMObject caller, object comProxy, bool throwException = true)
{
return this.GetFactoryInfo(HostCache, caller, comProxy, true, throwException);
return this.GetFactoryInfo(HostCache, caller, comProxy, throwException);
}

/// <summary>
Expand Down Expand Up @@ -766,166 +734,6 @@ public COMProxyShare CreateNewProxyShare(ICOMObject sender, object comProxy)
throw new CreateCOMProxyShareException(exception);
}
}

/// <summary>
/// Creates a new duck typing instance by given generic type argument
/// </summary>
/// <typeparam name="T">interface result type</typeparam>
/// <returns>new instance</returns>
/// <exception cref="ArgumentException">throws when ComProgIdAttribute is missing</exception>
/// <exception cref="DuckException">throws when its failed to compile an implementation</exception>
/// <exception cref="CreateInstanceException">throws when its failed to create new instance</exception>
/// <exception cref="COMException">throws when its failed to receive progID Type</exception>
[Obsolete("Support for dynamic objects will be removed in NetOffice 2.0")]
public T CreateDuckObject<T>() where T : ICOMObject
{
object[] attributes = typeof(T).GetCustomAttributes(typeof(NetOffice.Attributes.ComProgIdAttribute), false);
if (attributes.Length > 0)
{
NetOffice.Attributes.ComProgIdAttribute attribute = attributes[0] as NetOffice.Attributes.ComProgIdAttribute;
return CreateDuckObject<T>(attribute.Value);
}
else
throw new ArgumentException("ComProgIdAttribute is missing.");
}

/// <summary>
/// Creates a new duck typing instance by given generic type argument
/// </summary>
/// <typeparam name="T">interface result type</typeparam>
/// <param name="progId">progId to create</param>
/// <returns>new instance</returns>
/// <exception cref="ArgumentNullException">throws when progId is null or empty</exception>
/// <exception cref="DuckException">throws when its failed to compile an implementation</exception>
/// <exception cref="CreateInstanceException">throws when its failed to create new instance</exception>
/// <exception cref="COMException">throws when its failed to receive progID Type</exception>
[Obsolete("Support for dynamic objects will be removed in NetOffice 2.0")]
public T CreateDuckObject<T>(string progId) where T : ICOMObject
{
if (String.IsNullOrWhiteSpace(progId))
throw new ArgumentNullException("progId");
Type type = System.Type.GetTypeFromProgID(progId, false);
if (null == type)
throw new COMException("Unable to receive progId Type:<" + progId + ">");

object interopProxy = null;

try
{
interopProxy = Activator.CreateInstance(type);
}
catch (Exception exception)
{
throw new CreateInstanceException(exception);
}

return CreateDuckObjectFromComProxy<T>(interopProxy);
}

/// <summary>
/// Creates a new duck typing instance by given generic type argument
/// </summary>
/// <typeparam name="T">interface result type</typeparam>
/// <param name="caller"></param>
/// <param name="comProxy">new created proxy</param>
/// <returns>new instance</returns>
/// <exception cref="ArgumentNullException">throws when comProxy is null</exception>
/// <exception cref="DuckException">throws when its failed to compile an implementation</exception>
/// <exception cref="CreateInstanceException">throws when its failed to create new instance</exception>
[Obsolete("Support for dynamic objects will be removed in NetOffice 2.0")]
public T CreateDuckObjectFromComProxy<T>(ICOMObject caller, object comProxy) where T : ICOMObject
{
return (T)CreateDuckObjectFromComProxy(null, comProxy, typeof(T));
}

/// <summary>
/// Creates a new duck typing instance by given generic type argument
/// </summary>
/// <typeparam name="T">interface result type</typeparam>
/// <param name="comProxy">new created proxy</param>
/// <returns>new instance</returns>
/// <exception cref="ArgumentNullException">throws when comProxy is null</exception>
/// <exception cref="DuckException">throws when its failed to compile an implementation</exception>
/// <exception cref="CreateInstanceException">throws when its failed to create new instance</exception>
[Obsolete("Support for dynamic objects will be removed in NetOffice 2.0")]
public T CreateDuckObjectFromComProxy<T>(object comProxy) where T : ICOMObject
{
return (T)CreateDuckObjectFromComProxy(null, comProxy, typeof(T));
}

/// <summary>
/// Creates a new duck typing instance
/// </summary>
/// <param name="caller">parent there have created comProxy</param>
/// <param name="comProxy">new created proxy</param>
/// <returns>new instance</returns>
/// <exception cref="ArgumentNullException">throws when comProxy is null</exception>
/// <exception cref="DuckException">throws when its failed to compile an implementation</exception>
/// <exception cref="CreateInstanceException">throws when its failed to create new instance</exception>
/// <exception cref="FactoryException">throws when its failed to receive factory info</exception>
[Obsolete("Support for dynamic objects will be removed in NetOffice 2.0")]
public ICOMObject CreateDuckObjectFromComProxy(ICOMObject caller, object comProxy)
{
if (null == comProxy)
throw new ArgumentNullException("comProxy");

CheckInitialize();

IFactoryInfo factoryInfo = GetDuckFactoryInfo(caller, comProxy, true);
string className = TypeDescriptor.GetClassName(comProxy);
string fullClassName = factoryInfo.AssemblyNamespace + ".I" + className;

Type wrapperInterfaceType = factoryInfo.Assembly.GetType(fullClassName, true, true);
return CreateDuckObjectFromComProxy(caller, comProxy, wrapperInterfaceType);
}

/// <summary>
/// Creates a new duck typing instance which implement the given interfaces.
/// </summary>
/// <param name="caller">parent there have created comProxy</param>
/// <param name="comProxy">new created proxy</param>
/// <param name="wrapperInterfaceType">interface which is implemented by the returning instance. the interface must inherit from <see cref="ICOMObject"/></param>
/// <returns>new instance</returns>
/// <exception cref="ArgumentNullException">throws when comProxy, wrapperInterfaceType is null</exception>
/// <exception cref="DuckException">throws when its failed to compile an implementation</exception>
/// <exception cref="CreateInstanceException">throws when its failed to create new instance</exception>
[Obsolete("Support for dynamic objects will be removed in NetOffice 2.0")]
public ICOMObject CreateDuckObjectFromComProxy(ICOMObject caller, object comProxy, Type wrapperInterfaceType)
{
if (null == comProxy)
throw new ArgumentNullException("comProxy");
if (null == wrapperInterfaceType)
throw new ArgumentNullException("wrapperInterfaceType");

Type instanceType = null;
lock (_comObjectLock)
{
if (!DuckingCache.TryGetValue(wrapperInterfaceType, out instanceType))
{
try
{
DuckInterface proxyInterface = new DuckInterface(wrapperInterfaceType);
DuckTypeGenerator implementationTypeGenerator = new DuckTypeGenerator(proxyInterface);
instanceType = implementationTypeGenerator.GenerateType();
DuckingCache.Add(wrapperInterfaceType, instanceType);
}
catch (Exception exception)
{
throw new DuckException(exception);
}
}
}

try
{
ICOMObject newInstance = Activator.CreateInstance(instanceType, new object[] { this, caller, comProxy }) as ICOMObject;
return newInstance;
}
catch (Exception exception)
{
throw new CreateInstanceException(exception);
}
}

/// <summary>
/// Creates a new <see cref="ICOMObject"/> based on wrapperClassType
Expand Down
9 changes: 2 additions & 7 deletions Source/NetOffice/CoreExtensions/CoreFactoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ internal static class CoreFactoryExtensions
/// <param name="hostCache">core host cache</param>
/// <param name="caller">calling instance</param>
/// <param name="comProxy">new created proxy</param>
/// <param name="wantTheDuck">want duck implementation</param>
/// <param name="throwException">throw exception if no info found or return null</param>
/// <returns>factory info from corresponding assembly</returns>
internal static IFactoryInfo GetFactoryInfo(this Core value,
Dictionary<Guid, Guid> hostCache, ICOMObject caller,
object comProxy, bool wantTheDuck, bool throwException)
object comProxy, bool throwException)
{
if (value.Assemblies.Count == 0)
return null;
Expand All @@ -59,7 +58,7 @@ internal static IFactoryInfo GetFactoryInfo(this Core value,
{
foreach (IFactoryInfo item in value.Assemblies)
{
if (item.IsDuck != wantTheDuck || item.AssemblyName != caller.InstanceComponentName)
if (item.AssemblyName != caller.InstanceComponentName)
continue;
foreach (var guid in item.ComponentGuid)
if (true == guid.Equals(hostGuid))
Expand All @@ -70,8 +69,6 @@ internal static IFactoryInfo GetFactoryInfo(this Core value,
{
foreach (IFactoryInfo item in value.Assemblies)
{
if (item.IsDuck != wantTheDuck)
continue;
foreach (var guid in item.ComponentGuid)
if (true == guid.Equals(hostGuid))
return item;
Expand All @@ -82,8 +79,6 @@ internal static IFactoryInfo GetFactoryInfo(this Core value,
// list of known multiple defined types is available in Attributes\DuplicateAttribute.cs file
foreach (IFactoryInfo item in value.Assemblies)
{
if (item.IsDuck != wantTheDuck)
continue;
bool hasComponentID = null != item.ComponentGuid ? item.ComponentGuid.Contains(hostGuid) : false;
if (item.Contains(className) && hasComponentID)
{
Expand Down
Loading

0 comments on commit 0978c9a

Please sign in to comment.