1

I have the following operator in my Visual WebPart's codebehind file:

public static implicit operator TemplateControl
(ScholarChip.PaymentProcessor.PaymentProcessor.PaymentProcessor target)
    {
        return ((target == null) ? null : target.TemplateControl);
    }

I'm receiving the error:

User-defined conversion must convert to or from the enclosing type  

I'm unfamiliar with the above error. Can anyone suggest how I might rectify it? The code is being used in a Sharepoint 2010 Visual WebPart.

Thanks much:)

4

1 に答える 1

2

問題は、 と の間の変換をPaymentProcessor定義しているが、その変換をクラスでもクラスTemplateControlでも定義していないことです。TemplateClass はフレームワークの一部のように見えるので、そのクラスで変換を定義できるとは思えません。PaymentProcessorTemplateControl

ユーザー定義の変換は、変換される 2 つの型のいずれかのメンバーである必要があります (これは、エラー メッセージの内容を表す別の方法です)。つまり、PaymentProcessor クラスを制御する場合は、変換をそのクラスに移動します。どちらのクラスも制御していない場合は運が悪く、通常の方法で変換を処理する必要があります。

例として、これはあなたが見たのと同じコンパイラエラーを与えます:

class A {}
class B {}
class C
{
    public static implicit operator A(B source) { return new A(); }
}

これはコンパイルされます:

class A {}
class B
{
    public static implicit operator A(B source) { return new A(); }
}

これもそうです:

class A
{
    public static implicit operator A(B source) { return new A(); }
}
class B {}
于 2012-04-09T22:33:01.190 に答える