简体   繁体   English

如何定义不像类型那样使用的变量?

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

I have a code written in Visual Studio 2017.我有一个用 Visual Studio 2017 编写的代码。
Now I'm trying to use Visual Studio 2015, but Tuple installation failed.现在我正在尝试使用 Visual Studio 2015,但元组安装失败。
So I'm coding without using Tuple.所以我在不使用元组的情况下编码。

This code这段代码

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

causes原因

CS0118 : 'v' is a variable but is used like a type CS0118:“v”是一个变量,但用作类型

Original code in Visual Studio 2017 is 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();
        }
    }
}

and it works.它有效。

I don't know how to define 'v'.我不知道如何定义“v”。
How can I define variables that are not used like types?如何定义不像类型那样使用的变量?

你正在寻找这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM