簡體   English   中英

如何定義不像類型那樣使用的變量?

[英]How can I define variables that are not used like types?

我有一個用 Visual Studio 2017 編寫的代碼。
現在我正在嘗試使用 Visual Studio 2015,但元組安裝失敗。
所以我在不使用元組的情況下編碼。

這段代碼

namespace TEST00
{
    public class Class1<T> : IValueConverter
    {
        public T ValueForTrue { get; set; }

        public T ValueForFalse { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool v = new bool();
            bool v1 = (value is v) && v;
            return v1 ? ValueForTrue : ValueForFalse;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

原因

CS0118:“v”是一個變量,但用作類型

Visual Studio 2017 中的原始代碼是

namespace TEST00
{
    public class Class1<T> : IValueConverter
    {
        public T ValueForTrue { get; set; }

        public T ValueForFalse { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((value is bool v) && v) ? ValueForTrue : ValueForFalse;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

它有效。

我不知道如何定義“v”。
如何定義不像類型那樣使用的變量?

你正在尋找這個:

    bool v1 = (value is bool) && (bool)value;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM