3

调用了一个 .Net 对象,如下所示(cfdump 如下所示)......

<cfobject type=".NET" name="GoCardless" class="GoCardlessSdk.GoCardless" assembly="#expandpath("../GoCardlessSdk.dll")#,#expandpath("../Newtonsoft.Json.dll")#,#expandpath("../RestSharp.dll")#">

.Net 对象的 cfdump

我现在需要设置一个枚举。用于此的示例 .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的路径,我仍然会遇到相同的错误。

4

1 回答 1

4

(免责声明:我目前的测试环境是CF9,但结果和你的一样)

通常,您可以使用 修改字段set_FieldName( value )。但是,我可以从您的屏幕截图中看到该方法不存在。我不知道为什么,所以我做了一些搜索。根据我所阅读的内容,您的班级正在使用Nullable Types

    public static Environments? Environment { get; set; }

这似乎是问题的根源。据我所知,当涉及 Nullable 类型时,jnbridge(用于 .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>

测试类:(无可空类型)

namespace GoCardlessSdk
{
    public static class GoCardless
    {
        static GoCardless()
        {
        }

        public enum Environments
        {
            Production,
            Sandbox,
            Test
        }

        public static Environments Environment { get; set; }
    }
}

更新1:

  • 所以修改类以消除 Nullable 类型是一种选择
  • 另一种可能性是手动生成代理。jnbridge 也有 .net generics 的问题,因此自己生成代理可能会起作用。
  • 第三种可能性是编写一个帮助类来设置/获取值而不使用 Nullable 类型。这有点骇人听闻,但确实有效。

更新 2/3:

这是一个更简单的辅助类的粗略示例。如果您希望辅助get方法返回 null(如原始方法),请使用getNullableEnvironment. 如果您希望返回默认值而不是 null,请使用getEnvironment. 至于设置,我所做的只是:

  • 在 VS 中创建了一个新的解决方案
  • 添加了对 GoCardlessSDK.dll 的引用

<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;
        }
    }
}
于 2013-07-18T15:29:31.393 回答