Hi All,
Please excuse me, but the bulk of my post will be a code post. It
describes some weirdness with regards to the implicit casting operator.
The crux of the problem is this:
I want to set a property on a class that takes an interface instance.
I have a class that can cast implicit to a class that impliments said
interface,
but the compiler moans and says it cannot cast implicitly like that.
My question is why? Why does this make sense to work like this? Here is
the code:
BTW - the line marked with the **** comment gives this error: error
CS0266: Cannot implicitly convert type 'test_operators.WithOperator' to
'test_operators.IB'. An explicit conversion exists (are you missing a
cast?)
class Program
{
static void Main( string[] args )
{
A a = new A();
WithOperator w = new WithOperator();
a.TheIB = w; // **** - does not compile
a.TheIB = (BIml)w; // works
a.TheIB = new BImpl(); // works
}
}
interface IB
{
int SomeProperty { get; }
}
class A
{
public IB TheIB
{
set
{
Console.WriteLine( value.SomeProperty );
}
}
}
class BImpl : IB
{
#region IB Members
public int SomeProperty
{
get { return 10; }
}
#endregion
}
class WithOperator
{
public static implicit operator BImpl( WithOperator w )
{
return new BImpl();
}
}
Regards,
Pieter Breed