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() 呼び出しを削除すると、最初の 2 つの呼び出しは完全に正常に実行されます。すべてのcreateobject() 呼び出しにすべてのDLLへのパスを含めても、同じエラーが発生します。

4

1 に答える 1

4

(免責事項: 私の現在のテスト環境は CF9 ですが、結果はあなたのものと同じでした)

通常は、 を使用してフィールドを変更できますset_FieldName( value )。ただし、スクリーンショットから、メソッドが存在しないことがわかります。よくわからなかったので、ちょっと調べてみました。私が読んだことに基づいて、あなたのクラスは以下を使用していますNullable Types

    public static Environments? Environment { get; set; }

それが問題の根源のようです。私が知る限り、jnbridge ( .net 相互運用に使用される基礎となるツール) は、Nullable 型が関係している場合、プロキシを生成しないようです。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>

テスト クラス: (Null 許容型なし)

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

        public enum Environments
        {
            Production,
            Sandbox,
            Test
        }

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

更新 1:

  • したがって、クラスを変更して Nullable 型を排除することは 1 つのオプションです。
  • もう 1 つの可能性は、プロキシを手動で生成することです。jnbridgeは .net genericsにも問題があるため、自分でプロキシを生成するとうまくいくかもしれません
  • 3 番目の可能性は、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 に答える