Skip to content
Permalink
Browse files

Fix input/output COM object transformation.

  • Loading branch information
arabasso committed Nov 18, 2019
1 parent d772106 commit 2802cb3d1a6173f6eed7915a817e39d55c11b7d5
Showing with 32 additions and 34 deletions.
  1. +32 −34 ComObject/ComObject.cs
@@ -44,37 +44,25 @@ public class ComObject
SetMemberBinder binder,
object value)
{
Type.SetProperty(binder.Name, Object, value is ComObject o ? o.Object : value);
Type.SetProperty(binder.Name, Object, TransformValue(value));

return true;
}

private readonly HashSet<ComObject> _objects = new HashSet<ComObject>();

public override bool TryGetIndex(
GetIndexBinder binder,
object[] indexes,
out object result)
{
result = Type.GetIndex(Object, indexes);

if (result != null && !IsPrimitive(result))
{
_objects.Add((ComObject)(result = new ComObject(result)));
}
result = TransformValue(Type.GetIndex(Object, indexes));

return true;
}

public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
result = Type.GetProperty(binder.Name, Object);

if (result != null && !IsPrimitive(result))
{
_objects.Add((ComObject)(result = new ComObject(result)));
}
result = TransformValue(Type.GetProperty(binder.Name, Object));

return true;
}
@@ -84,33 +72,33 @@ public class ComObject
object[] args,
out object result)
{
result = Type.InvokeMethod(binder.Name, Object, TransformArguments(args));

if (result != null && !IsPrimitive(result))
{
_objects.Add((ComObject)(result = new ComObject(result)));
}
result = TransformValue(Type.InvokeMethod(binder.Name, Object, args.Select(TransformValue).ToArray()));

return true;
}

private object[] TransformArguments(
IEnumerable args)
private object TransformValue(
object value)
{
return args.Select(s =>
if (value == null) return null;

if (IsComObject(value))
{
switch (s)
{
case ComObject o:
return o.Object;
_objects.Add((ComObject)(value = new ComObject(value)));
}

else switch (value)
{
case IEnumerable a when !IsPrimitive(value):
value = a.Select(TransformValue).ToArray();
break;

case IEnumerable a:
return IsPrimitive(s) ? s : TransformArguments(a);
case ComObject o:
value = o.Object;
break;
}

default:
return s;
}
}).ToArray();
return value;
}

public override bool TryConvert(
@@ -122,6 +110,14 @@ public class ComObject
return true;
}

private bool IsComObject(
object o)
{
var t = o.GetType();

return t.Name == "__ComObject";
}

private bool IsPrimitive(
object o)
{
@@ -158,5 +154,7 @@ public void Dispose()

Marshal.ReleaseComObject(Object);
}

private readonly HashSet<ComObject> _objects = new HashSet<ComObject>();
}
}

0 comments on commit 2802cb3

Please sign in to comment.
You can’t perform that action at this time.