不可调用成员 'Money.CurrencyEnum' 不能像方法一样使用

Non-Invocable member 'Money.CurrencyEnum' cannot be used like a method

我正在尝试实施平方以产生费用,但在第

Money money = new Money(amount, Money.CurrencyEnum(currency));

我不断收到以下错误并且无法弄清楚原因:

Non-Invocable member 'Money.CurrencyEnum'  cannot be used like a method.

感谢任何想法

using System;
using System.Diagnostics;
using Square.Connect.Api;
using Square.Connect.Client;
using Square.Connect.Model;

namespace Tester2
{
    public class Example
    {
        // Retrieving your location IDs
        public static void RetrieveLocations()
        {
            LocationApi _locationApi = new LocationApi();
            string authorization = "YOUR_ACCESS_TOKEN";
            var response = _locationApi.ListLocations(authorization);
        }

        // Charge the card nonce
        public static void ChargeNonce()
        {
            // Every payment you process for a given business have a unique idempotency key.
            // If you're unsure whether a particular payment succeeded, you can reattempt
            // it with the same idempotency key without worrying about double charging
            // the buyer.
            string idempotencyKey = Guid.NewGuid().ToString();

            // Monetary amounts are specified in the smallest unit of the applicable currency.
            // This amount is in cents. It's also hard-coded for , which is not very useful.
            int amount = 100;
            string currency = "USD";
            Money money = new Money(amount, Money.CurrencyEnum(currency));

            string nonce = "YOUR_NONCE";
            string authorization = "YOUR_ACCESS_TOKEN";
            string locationId = "YOUR_LOCATION_ID";
            ChargeRequest body = new ChargeRequest(AmountMoney: money, IdempotencyKey: idempotencyKey, CardNonce: nonce);
            TransactionApi transactionApi = new TransactionApi();
            var response = transactionApi.Charge(authorization, locationId, body);
        }
    }
}

您正在尝试访问一个枚举,就像它是一个方法一样。如果您想将字符串转换为枚举值,可以使用 Enum.TryParse:

if(Enum.TryParse("USD", out Money.CurrencyEnum value))
{
    Money money = new Money(amount, value);
}

你得到这个错误是因为 Money.CurrencyEnum 可能是 属性 而不是一个方法,所以你不能像这样调用它:

Money.CurrencyEnum(currency)

问题是基于它实际是什么。如果是属性,就用这个:

Money.CurrencyEnum

如果它是子class枚举那么你可以解析你的货币值并将其转换为你的枚举类型:

var e = (Money.CurrencyEnum)Enum.Parse(typeof(Money.CurrencyEnum), currency);

Money.CurrencyEnum是枚举类型。这不是一种方法。因此,您不能像调用方法一样调用它。

在你得到的情况下,你似乎知道你想要哪个值,所以就用它吧:

Money money = new Money(amount, Money.CurrencyEnum.USD);

如果必须解析字符串,试试这个:

var strCurr = "USD";
Money.CurrencyEnum currency;

if (!Enum.TryParse(strCurr, out currency))
{
    throw new Exception("Bad currency type: " + strCurr);
}

Money money = new Money(amount, currency);