.net - 如何在 ColdFusion 中设置 .NET 枚举

标签 .net coldfusion coldfusion-10

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

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

cfdump of .Net object

我现在需要设置一个枚举。示例 .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() 调用,前两个运行得很好。即使我在 all createobject() 调用中包含了 all DLL 的路径,我仍然会遇到同样的错误。

最佳答案

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

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

    public static Environments? Environment { get; set; }

这似乎是问题的根源。据我所知,当涉及可空类型时,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>

测试类:(无可空类型)

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

        public enum Environments
        {
            Production,
            Sandbox,
            Test
        }

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

更新 1:

  • 因此修改类以消除 Nullable 类型是一种选择
  • 另一种可能性是 generate the proxies manually . jnbridge 也 has issues with .net generics ,因此您自己生成代理可能可行。
  • 第三种可能性是编写一个辅助类来设置/获取值而不使用 Nullable 类型。这有点 hack,但确实有效。

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;
        }
    }
}

关于.net - 如何在 ColdFusion 中设置 .NET 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17723113/

相关文章:

c# - C# 或 .NET 中的程序集到底是什么?

c# - Visual Studio 2015 上缺少引用错误

.NET 4.0 应用程序在没有 Visual Studio 的机器上抛出异常

coldfusion - 如何在 CF10 中确定闭包变量的范围?

rest - 可以在 CF10 中使用 REST 代替 URL Rewrite 吗?

cookies - 如何在 ColdFusion 10 中覆盖 CFID/CFTOKEN?

c# - 更改系统参数

regex - Coldfusion 替换 "&"但不是 htmlspecialchars

ColdFusion 11 没有关闭 java.io.FileInputStream

apache - 将 web.config 重写规则转换为 htaccess 重写规则(所有不带扩展名的文件将被处理为 .cfm)