调用.Net对象如下所示(cfdump如下所示)......
<cfobject type=".NET" name="GoCardless" class="GoCardlessSdk.GoCardless" assembly="#expandpath("../GoCardlessSdk.dll")#,#expandpath("../Newtonsoft.Json.dll")#,#expandpath("../RestSharp.dll")#">
我现在需要设置一个枚举。这个示例.Net代码是:
GoCardless.Environment = GoCardless.Environments.Sandbox;
该课程的C#代码如下所示:
using System;
using System.Collections.Generic;
using System.Reflection;
using GoCardlessSdk.Api;
using GoCardlessSdk.Connect;
using GoCardlessSdk.Helpers;
using GoCardlessSdk.Partners;
namespace GoCardlessSdk
{
public static class GoCardless
{
static GoCardless()
{
AccountDetails = new AccountDetails();
}
public enum Environments
{
Production,
Sandbox,
Test
}
public static readonly Dictionary<Environments, string> BaseUrls =
new Dictionary<Environments, string>
{
{Environments.Production, "https://gocardless.com"},
{Environments.Sandbox, "https://sandbox.gocardless.com"},
{Environments.Test, "http://gocardless.com"}
};
private static string _baseUrl;
public static string BaseUrl
{
get { return _baseUrl ?? BaseUrls[Environment ?? Environments.Production]; }
set { _baseUrl = value.Trim(); }
}
public static Environments? Environment { get; set; }
public static AccountDetails AccountDetails { get; set; }
public static ApiClient Api
{
get { return new ApiClient(AccountDetails.Token); }
}
public static ConnectClient Connect
{
get { return new ConnectClient(); }
}
public static PartnerClient Partner
{
get { return new PartnerClient(); }
}
internal static string UserAgent = GetUserAgent();
private static string GetUserAgent()
{
try
{
return "gocardless-dotnet/v" + GetAssemblyFileVersion();
}
catch
{
return "gocardless-dotnet";
}
}
private static string GetAssemblyFileVersion()
{
Assembly assembly = Assembly.GetAssembly(typeof (GoCardless));
var attributes = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
as AssemblyFileVersionAttribute[];
if (attributes != null && attributes.Length == 1)
{
return attributes[0].Version;
}
return "";
}
private static Func<string> _generateNonce;
internal static Func<string> GenerateNonce
{
get { return _generateNonce ?? (_generateNonce = Utils.GenerateNonce); }
set { _generateNonce = value; }
}
private static Func<DateTimeOffset> _getUtcNow;
internal static Func<DateTimeOffset> GetUtcNow
{
get { return _getUtcNow ?? (_getUtcNow = () => DateTimeOffset.UtcNow); }
set { _getUtcNow = value; }
}
}
}
有人能够向我解释如何在ColdFusion中设置枚举吗?
谢谢!
更新
为了回应Leigh的实用类解决方案,我的代码如下:
<cfscript>
GoCardless = createObject(".net", "GoCardlessSdk.GoCardless","path to GoCardless DLL,paths to supporting DLLs");
Environments = createObject(".net", "GoCardlessSdk.GoCardless$Environments", "path to GoCardless DLL");
util = createObject(".net", "GoCardlessSdk.GoCardlessSdkUtil", "path to utility DLL");
dumps etc...
</cfscript>
如果我删除最后一个createobject()调用,前两个运行完全正常。即使我在所有 createobject()调用中包含所有 DLL的路径,我仍然会遇到相同的错误。
答案 0 :(得分:4)
(免责声明:我目前的测试环境是CF9,但结果与您的相同)
通常,您可以使用set_FieldName( value )
修改字段。但是,我可以从你的屏幕截图中看到该方法不存在。我不知道为什么,所以我做了一点搜索。根据我所读到的内容,您的课程正在使用Nullable Types
:
public static Environments? Environment { get; set; }
这似乎是问题的根源。据我所知,当涉及Nullable Types时,jnbridge(underlying tool used for .net interop)似乎不会生成代理。这可以解释为什么缺少访问Evironment
字段的方法。如果您删除了?
运算符,它就可以正常运行。
CF代码:
<cfscript>
goCardless = createObject(".net", "GoCardlessSdk.GoCardless", ExpandPath("./GoCardlessSdk.dll"));
Environments = createObject(".net", "GoCardlessSdk.GoCardless$Environments", ExpandPath("./GoCardlessSdk.dll"));
goCardLess.set_Environment(Environments.Test);
writeDump( goCardLess.get_Environment().toString() );
</cfscript>
测试类:(不可为Nullable类型)
namespace GoCardlessSdk
{
public static class GoCardless
{
static GoCardless()
{
}
public enum Environments
{
Production,
Sandbox,
Test
}
public static Environments Environment { get; set; }
}
}
更新1:
更新2/3:
这是一个更简单的助手类的粗略示例。如果您希望帮助器get
方法返回null(与原始方法一样),请使用getNullableEnvironment
。如果您希望返回默认值而不是null,请使用getEnvironment
。就我所做的所有设置而言:
<cfscript>
dllPaths = arrayToList( [ ExpandPath("GoCardlessUtil.dll")
, ExpandPath("GoCardlessSdk.dll")
, ExpandPath("Newtonsoft.Json.dll")
, ExpandPath("RestSharp.dll")
]
);
goCardless = createObject(".net", "GoCardlessSdk.GoCardless", dllPaths);
Environments = createObject(".net", "GoCardlessSdk.GoCardless$Environments", dllPaths);
util = createObject(".net", "GoCardlessUtil.GoCardlessSdkUtil", dllPaths );
WriteDump("before="& util.getNullableEnvironment());
util.setEnvironment(Environments.Production);
WriteDump("after="& util.getNullableEnvironment().toString());
</cfscript>
<强> GoCardlessSdkUtil.cs 强>
using System;
using System.Collections.Generic;
using System.Text;
using GoCardlessSdk;
namespace GoCardlessUtil
{
public class GoCardlessSdkUtil
{
public static void setEnvironment(GoCardless.Environments environ)
{
GoCardless.Environment = environ;
}
/*
// Enum's cannot be null. So we are applying a default
// value ie "Test" instead of returning null
public static GoCardless.Environments getEnvironment()
{
return GoCardless.Environment.HasValue ? GoCardless.Environment.Value : GoCardless.Environments.Test;
}
*/
// This is the closest to the original method
// Since enum's cannot be null, we must return type Object instead of Environment
// Note: This will return be null/undefined the first time it is invoked
public static Object getNullableEnvironment()
{
return GoCardless.Environment;
}
}
}