-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
940 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System; | ||
using SapNwRfc.Internal.Fields; | ||
using SapNwRfc.Internal.Interop; | ||
|
||
namespace SapNwRfc.Internal.Dynamic | ||
{ | ||
internal static class DynamicRfc | ||
{ | ||
internal static bool TryGetRfcValue(RfcInterop interop, IntPtr dataHandle, string name, SapRfcType type, Func<ISapTypeMetadata> typeMetadata, Type returnType, out object result) | ||
{ | ||
result = GetRfcValue(interop, dataHandle, name, type, typeMetadata); | ||
|
||
if (returnType == typeof(object)) | ||
{ | ||
return true; | ||
} | ||
|
||
try | ||
{ | ||
result = Convert.ChangeType(result, returnType); | ||
return true; | ||
} | ||
catch { } | ||
|
||
result = null; | ||
return false; | ||
} | ||
|
||
private static object GetRfcValue(RfcInterop interop, IntPtr dataHandle, string name, SapRfcType type, Func<ISapTypeMetadata> typeMetadata) | ||
{ | ||
switch (type) | ||
{ | ||
case SapRfcType.RFCTYPE_CHAR: | ||
case SapRfcType.RFCTYPE_NUM: | ||
case SapRfcType.RFCTYPE_BCD: | ||
case SapRfcType.RFCTYPE_STRING: | ||
return StringField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_INT: | ||
case SapRfcType.RFCTYPE_INT1: | ||
case SapRfcType.RFCTYPE_INT2: | ||
return IntField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_INT8: | ||
return LongField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_FLOAT: | ||
return DoubleField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_DECF16: | ||
case SapRfcType.RFCTYPE_DECF34: | ||
return DecimalField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_DATE: | ||
return DateField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_TIME: | ||
return TimeField.Extract(interop, dataHandle, name).Value; | ||
|
||
case SapRfcType.RFCTYPE_BYTE: | ||
case SapRfcType.RFCTYPE_XSTRING: | ||
return BytesField.Extract(interop, dataHandle, name, bufferLength: 0).Value; | ||
|
||
case SapRfcType.RFCTYPE_TABLE: | ||
{ | ||
RfcResultCode resultCode = interop.GetTable( | ||
dataHandle: dataHandle, | ||
name: name, | ||
tableHandle: out IntPtr tableHandle, | ||
errorInfo: out RfcErrorInfo errorInfo); | ||
|
||
resultCode.ThrowOnError(errorInfo); | ||
|
||
return new DynamicRfcTable(interop, tableHandle, typeMetadata()); | ||
} | ||
|
||
case SapRfcType.RFCTYPE_STRUCTURE: | ||
{ | ||
RfcResultCode resultCode = interop.GetStructure( | ||
dataHandle: dataHandle, | ||
name: name, | ||
structHandle: out IntPtr structHandle, | ||
errorInfo: out RfcErrorInfo errorInfo); | ||
|
||
resultCode.ThrowOnError(errorInfo); | ||
|
||
return new DynamicRfcStructure(interop, structHandle, typeMetadata()); | ||
} | ||
|
||
case SapRfcType.RFCTYPE_NULL: | ||
return null; | ||
} | ||
|
||
throw new NotSupportedException($"Parameter type {type} is not supported"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using SapNwRfc.Internal.Interop; | ||
|
||
namespace SapNwRfc.Internal.Dynamic | ||
{ | ||
internal sealed class DynamicRfcFunction : DynamicObject, IReadOnlyDictionary<string, object> | ||
{ | ||
private readonly RfcInterop _interop; | ||
private readonly IntPtr _functionHandle; | ||
private readonly ISapFunctionMetadata _functionMetadata; | ||
|
||
internal DynamicRfcFunction(RfcInterop interop, IntPtr functionHandle, ISapFunctionMetadata functionMetadata) | ||
{ | ||
_interop = interop; | ||
_functionHandle = functionHandle; | ||
_functionMetadata = functionMetadata; | ||
} | ||
|
||
private bool TryGetByName(string name, Type returnType, out object result) | ||
{ | ||
if (_functionMetadata.Parameters.TryGetValue(name, out ISapParameterMetadata parameter)) | ||
{ | ||
if (DynamicRfc.TryGetRfcValue(_interop, _functionHandle, parameter.Name, parameter.Type, parameter.GetTypeMetadata, returnType, out result)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
|
||
private bool TryGetByIndex(int index, Type returnType, out object result) | ||
{ | ||
if (index >= 0 && index < _functionMetadata.Parameters.Count) | ||
{ | ||
ISapParameterMetadata parameter = _functionMetadata.Parameters[index]; | ||
|
||
if (DynamicRfc.TryGetRfcValue(_interop, _functionHandle, parameter.Name, parameter.Type, parameter.GetTypeMetadata, returnType, out result)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
|
||
public override IEnumerable<string> GetDynamicMemberNames() | ||
{ | ||
return Keys; | ||
} | ||
|
||
public override bool TryGetMember(GetMemberBinder binder, out object result) | ||
{ | ||
return TryGetByName(binder.Name, binder.ReturnType, out result); | ||
} | ||
|
||
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) | ||
{ | ||
if (indexes.Length != 1) | ||
throw new ArgumentOutOfRangeException(nameof(indexes)); | ||
|
||
var index = indexes[0]; | ||
|
||
if (index is string name) | ||
return TryGetByName(name, binder.ReturnType, out result); | ||
|
||
if (index is int i) | ||
return TryGetByIndex(i, binder.ReturnType, out result); | ||
|
||
if (index is uint u) | ||
return TryGetByIndex((int)u, binder.ReturnType, out result); | ||
|
||
throw new ArgumentException(nameof(indexes)); | ||
} | ||
|
||
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) | ||
{ | ||
if (binder.Name == "GetMetadata" && binder.CallInfo.ArgumentCount == 0) | ||
{ | ||
result = _functionMetadata; | ||
return true; | ||
} | ||
|
||
return base.TryInvokeMember(binder, args, out result); | ||
} | ||
|
||
public IEnumerable<string> Keys | ||
{ | ||
get | ||
{ | ||
foreach (var parameter in _functionMetadata.Parameters) | ||
{ | ||
yield return parameter.Name; | ||
} | ||
} | ||
} | ||
|
||
public IEnumerable<object> Values | ||
{ | ||
get | ||
{ | ||
foreach (var parameter in _functionMetadata.Parameters) | ||
{ | ||
if (TryGetByName(parameter.Name, typeof(object), out object result)) | ||
{ | ||
yield return result; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public int Count => _functionMetadata.Parameters.Count; | ||
|
||
public object this[string key] | ||
{ | ||
get | ||
{ | ||
if (TryGetByName(key, typeof(object), out var result)) | ||
{ | ||
return result; | ||
} | ||
|
||
throw new KeyNotFoundException(); | ||
} | ||
} | ||
|
||
public bool ContainsKey(string key) | ||
{ | ||
return _functionMetadata.Parameters.TryGetValue(key, out _); | ||
} | ||
|
||
public bool TryGetValue(string key, out object value) | ||
{ | ||
return TryGetByName(key, typeof(object), out value); | ||
} | ||
|
||
public IEnumerator<KeyValuePair<string, object>> GetEnumerator() | ||
{ | ||
foreach (var parameter in _functionMetadata.Parameters) | ||
{ | ||
if (TryGetByName(parameter.Name, typeof(object), out object result)) | ||
{ | ||
yield return new KeyValuePair<string, object>(parameter.Name, result); | ||
} | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
} |
Oops, something went wrong.