Tag: 隐式转换

为什么不能在C#中共存相同类型的隐式和显式运算符?

为什么不能在同一个类中共存两个相同类型的运算符(显式和隐式)? 假设我有以下内容: public class Fahrenheit { public float Degrees { get; set; } public Fahrenheit(float degrees) { Degrees = degrees; } public static explicit operator Celsius(Fahrenheit f) { return new Celsius(ToCelsius(f.Degrees)); } public static implicit operator Celsius(Fahrenheit f) { return new Celsius(ToCelsius(f.Degrees)); } } public class Celsius { public float Degrees { get; set; } […]

你能用隐式转换满足generics约束吗?

鉴于以下类型: class A { } class B { public static implicit operator A(B me) { return new A(); } } class Test where T : A { } 我试过了 var b = new Test(); 并期望它失败,它做到了。 但错误信息是 类型’B’不能用作generics类型或方法’Test’中的类型参数’T’。 没有从’B’到’A’的隐式引用转换。 但是有从B到A的隐式引用转换。这只是一个奇怪的消息吗? 亚当罗宾逊的答案显示, 没有隐含的参考转换。 消息是正确的。 请注意, MSDN说: 其中T :(基类名) – 类型参数必须是或从指定的基类派生。 这解释了为什么不允许它,因为B不是从A派生A

generics类型的隐式运算符

使用隐式运算符有什么问题,如下所示: //linqpad c# program example void Main() { var testObject = new MyClass() { Value = 1 }; var add = 10 + testObject; //implicit conversion to int here add.Dump(); // 11 } class MyClass { public T Value { get; set; } public static implicit operator T (MyClass myClassToConvert) { return myClassToConvert.Value; } } […]

为什么类型推断和隐式运算符在以下情况下不起作用?

我将试着用一个例子来解释我的问题: class V { public readonly Func Get; public readonly bool IsConstant; V(Func get, bool isConstant) { Get = get; IsConstant = isConstant; } public static implicit operator V(T value) { return new V(() => value, true); } public static implicit operator V(Func getter) { return new V(getter, false); } } void DoSomething(V v) { […]

为什么C#中的隐式类型转换失败?

背景: 我们假设我有以下课程: class Wrapped : IDisposable { public Wrapped(T obj) { /* … */ } public static implicit operator Wrapped(T obj) { return new Wrapped(obj); } public void Dispose() { /* … */ } } 如您所见,它为T → Wrapped提供了隐式类型转换运算符。 最后,我希望能够使用这个类如下: interface IX { /* … */ } class X : IX { /* … */ } […]

将方法组隐式转换为Delegate(对于Control.Invoke的参数)

我正在使用Windows窗体应用程序,它包含自定义控件,这些控件可能会从UI线程以外的线程调用。 因此,这些方法看起来有点像这样以防止exception: public void DoSomeStuff() { if (InvokeRequired) { Invoke((Action)DoSomeStuff); } else { // Actually do some stuff. } } 方法组DoSomeStuff对Action的显式DoSomeStuff引起了我的注意,因此我一直在深入研究代表和其他相关主题。 虽然我在这里看到了一些相关的问题,但我还是找不到我的答案,这是: 为什么方法组DoSomeStuff在这种情况下需要显式转换为Action ? 如果我删除了演员,那么我会得到两个错误: 错误102参数1:无法从’方法组’转换为’System.Delegate’ 错误101’System.Windows.Forms.Control.Invoke(System.Delegate,params object [])’的最佳重载方法匹配有一些无效的参数 事实上编译器显然混淆了使用Invoke哪个重载似乎是一个相当大的提示,但我仍然不确定为什么它无法解决这个问题。 我希望编译器推断出Invoke的第一个重载是一个应该使用的,它接受一个Delegate参数。 我希望如果代码是这样编写的话没有问题: Action a = DoSomeStuff; Invoke(a); 方法组DoSomeStuff可以隐式转换为Action委托类型, Action从System.Delegate派生(技术上?),因此Invoke可以毫无困难地处理参数a 。 但是当我尝试直接将DoSomeStuff作为参数传递时,为什么编译器无法进行隐式转换呢? 说实话,我不相信我自己的逻辑,但我仍然不确定我错过了什么。

如何从reflection中执行显式操作转换?

我想使用reflection,并使用reflection进行隐式或显式转换。 鉴于我已经用这种方式定义了Foo public class Foo { public static explicit operator decimal(Foo foo) { return foo.Value; } public static explicit operator Foo(decimal number) { return new Foo(number); } public Foo() { } public Foo(decimal number) { Value = number; } public decimal Value { get; set; } public override string ToString() { return Value.ToString(); } } […]

隐式算子

我刚刚看到它在最近的一个答案中使用: public static implicit operator bool(Savepoint sp) { return sp != null; } 为什么我们在这里需要含义,这是什么意思?

c#参数隐式转换

有这个代码: class Program { static void Main(string[] args) { Check(3); Console.ReadLine(); } static void Check(int i) { Console.WriteLine(“I am an int”); } static void Check(long i) { Console.WriteLine(“I am a long”); } static void Check(byte i) { Console.WriteLine(“I am a byte”); } } 为什么这段代码打印“我是一个int”而不是“我很长”?

如何确定C#中是否存在隐式转换?

我有两种类型,T和U,我想知道是否从T到U定义了隐式强制转换运算符。 我知道IsAssignableFrom的存在,这不是我正在寻找的,因为它不处理隐式转换。 一些谷歌搜索引导我到这个解决方案 ,但在作者自己的话中,这是丑陋的代码(它试图隐式转换,如果有exception则返回false,否则为true) 似乎测试是否存在具有正确签名的op_Implicit方法将不适用于基本类型 。 是否有更简洁的方法来确定隐式转换运算符的存在?