Skip to content
107 changes: 69 additions & 38 deletions dotnet/src/dotnetframework/GxClasses/Helpers/GxDynamicCall.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,98 @@
using GeneXus.Application;
using GeneXus.Metadata;
using GeneXus.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using GeneXus.Application;
using GeneXus.Metadata;
using GeneXus.Utils;
using GxClasses.Helpers;
using Microsoft.IdentityModel.Tokens;

namespace GeneXus.DynamicCall
{
public class GxDynamicCall
{
public class GxDynamicCall
{
private const string defaultMethod = "execute";
private const string AssemblyNameProperty = "AssemblyName";
private const string NamespaceProperty = "Namespace";
private const string DefaultNamespace = "GeneXus.Programs";
private Assembly _assembly;
private readonly IGxContext _context;
private GXProperties _extendedProperties;
private GxDynCallProperties _properties;
private object _object;

//[Obsolete("ObjectName is deprecated. Use ExternalName instead", false)]
public string ObjectName { get; set; }
public GxDynCallProperties Properties
public string ExternalName { get; set; }

//[Obsolete("Properties is deprecated. Use ExtendedProperties instead", false)]
public GxDynCallProperties Properties
{
get => _properties;
set
{
_properties = Properties;
_properties = value;
}
}

public GXProperties ExtendedProperties
{
get => _extendedProperties;
set
{
_extendedProperties = value;
}
}
public GxDynamicCall()
{
_assembly= null;
_extendedProperties = new GXProperties();
_properties = new GxDynCallProperties();
}

public GxDynamicCall(IGxContext context)
{
_context = context;
_assembly = null;
_extendedProperties = new GXProperties();
_properties = new GxDynCallProperties();
_object = null;
}

private void VerifyDefaultProperties() {
_properties.NameSpace = string.IsNullOrEmpty(_properties.NameSpace) ? "GeneXus.Programs" : _properties.NameSpace;
private void VerifyDefaultProperties()
{

if (_assembly is null)
{
if (string.IsNullOrEmpty(_properties.AssemblyName))
if (string.IsNullOrEmpty(_extendedProperties.Get(AssemblyNameProperty)))
{
#if NETCORE
var stackTrace = new StackTrace();
var frame = stackTrace.GetFrame(5);
var method = frame.GetMethod();
var assembly = method.DeclaringType.Assembly;
_assembly = assembly;
#else
_assembly = Assembly.GetCallingAssembly();
#endif
}
else
{
try
{
_assembly = Assembly.LoadFrom(_properties.AssemblyName);
#if NETCORE
_assembly = AssemblyLoader.LoadAssembly(new AssemblyName(_extendedProperties.Get(AssemblyNameProperty)));
#else
_assembly = Assembly.LoadFrom(_extendedProperties.Get(AssemblyNameProperty) + ".dll" );
#endif
}
catch
catch (Exception e)
{
throw;
throw new InvalidOperationException("Error loading assembly: " + _extendedProperties.Get(AssemblyNameProperty), e);
}
}
}
}

public void Execute(ref IList<object> parameters, out IList<SdtMessages_Message> errors)
{
Create(null, out errors);
Expand All @@ -63,24 +103,19 @@ public void Execute(ref IList<object> parameters, out IList<SdtMessages_Message>
}
}

public void Create( IList<object> constructParms, out IList<SdtMessages_Message> errors)
public void Create(IList<object> constructParms, out IList<SdtMessages_Message> errors)
{
errors = new GXBaseCollection<SdtMessages_Message>();
string objectNameToInvoke;
try
{
VerifyDefaultProperties();
if (constructParms is null)
{
objectNameToInvoke = ObjectName;
}
else
{
objectNameToInvoke = _properties.ExternalName;
}

objectNameToInvoke = string.IsNullOrEmpty(_extendedProperties.Get(NamespaceProperty)) ? DefaultNamespace + "." + ExternalName.ToLower().Trim() : _extendedProperties.Get(NamespaceProperty) + "." + ExternalName.ToLower().Trim();

try
{
Type objType = ClassLoader.FindType(objectNameToInvoke, _properties.NameSpace, objectNameToInvoke.ToLower().Trim(), _assembly);
Type objType = ClassLoader.FindType(_assembly.GetName().Name, string.IsNullOrEmpty(_extendedProperties.Get(NamespaceProperty))? DefaultNamespace : _extendedProperties.Get(NamespaceProperty), ExternalName.ToLower().Trim(), _assembly);
object[] constructorParameters;
if (constructParms != null && constructParms.Count > 0)
{
Expand All @@ -89,27 +124,27 @@ public void Create( IList<object> constructParms, out IList<SdtMessages_Message>
}
else
{
constructorParameters = new object[] { new GxContext() };
constructorParameters = new object[] { _context };
}
_object = Activator.CreateInstance(objType, constructorParameters);
}
catch (Exception e)
{
GXUtil.ErrorToMessages("CreateInstance Error", e.Message, (GXBaseCollection<SdtMessages_Message>) errors);
GXUtil.ErrorToMessages("CreateInstance Error", e.Message, (GXBaseCollection<SdtMessages_Message>)errors);
}
}
catch (Exception e)
{
GXUtil.ErrorToMessages("VerifyProperties Error", e.Message, (GXBaseCollection<SdtMessages_Message>)errors);
}
}
public object Execute(ref IList<object> parameters, GxDynCallMethodConf methodconfiguration , out IList<SdtMessages_Message> errors)
public object Execute(ref IList<object> parameters, GxDynCallMethodConf methodconfiguration, out IList<SdtMessages_Message> errors)
{
object result;
errors = new GXBaseCollection<SdtMessages_Message>();
IList<object> outParms= new List<object>();
IList<object> outParms = new List<object>();

string methodName = methodconfiguration.MethodName;
string methodName = methodconfiguration.MethodName;
bool isStatic = methodconfiguration.IsStatic;

if (!isStatic)
Expand All @@ -118,7 +153,7 @@ public object Execute(ref IList<object> parameters, GxDynCallMethodConf methodco
{
try
{
outParms = ReflectionHelper.CallMethod(_object, (string.IsNullOrEmpty(methodName) ? defaultMethod : methodName), parameters);
outParms = ReflectionHelper.CallMethod(_object, string.IsNullOrEmpty(methodName) ? defaultMethod : methodName, parameters);
}
catch (Exception e)
{
Expand All @@ -133,7 +168,7 @@ public object Execute(ref IList<object> parameters, GxDynCallMethodConf methodco
else
{
VerifyDefaultProperties();
Type objType = ClassLoader.FindType(_properties.ExternalName, _properties.NameSpace, _properties.ExternalName.ToLower().Trim(), _assembly);
Type objType = ClassLoader.FindType(ExternalName, ExternalName.ToLower().Trim(), _assembly);
outParms = ReflectionHelper.CallMethod(objType, (string.IsNullOrEmpty(methodName) ? defaultMethod : methodName), parameters, isStatic);
}
if (outParms.Count > parameters.Count)
Expand Down Expand Up @@ -167,9 +202,7 @@ public GxDynCallMethodConf()
IsStatic = false;
MethodName = "execute";
}

}

public class GxDynCallProperties
{
public string ExternalName
Expand All @@ -187,7 +220,5 @@ public string NameSpace
get;
set;
}

}
}

}
Loading
Loading