2016-06-15 12 views
0

私はポイントのようなものを得るために使用する必要がある、異なる機能を持つ次のクラス/タイプを持っています。カスタムコンバータでCS1503を解決する方法

[TypeConverter(typeof(KnotenConverter))] 
class Knoten 
{ 
    int x, y; 
    List<Knoten> neighbors; 


    #region gettersetter 
    //Standard getter and setter here 

    #endregion 

    public bool hasNeighbors() 
    { 
     return Neighbors.Count > 0; 
    } 

    class KnotenConverter : TypeConverter 
    { 
     public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
     { 
      if (destinationType == typeof(Point)) return true; 
      return base.CanConvertTo(context, destinationType); 
     } 

     public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
     { 
      Knoten from = (Knoten)value; 
      if (destinationType == typeof(Point)) 
      { 
       return new Point(from.X, from.Y); 
      } 
      return base.ConvertTo(context, culture, value, destinationType); 
     } 

    } 

Knotenをポイントとして使用するために、自分自身のTypeConverterを実装してKnotenをPointに変換しました。しかし、私は得続ける

CS1503引数 '2'はGridCalcer.KnotenからSystem.Drawing.Pointに変換できません。

何が私は間違っていましたか、どうやってこのエラーを解決して、Knotenをポイントに変換することができますか?

+1

たラインで:それを行うことができるようにするために、あなたはimplicit変換演算子を宣言する必要がありますか?それはコンバータのラインですか?どの線?または、たとえば値を設定するときに発生します。 'PropertyGrid'? – Sinatr

答えて

1

TypeConverterは、コンパイル時には何も共通点がありません。コンパイラエラーは、​​インスタンスをPointが必要なメソッドに渡そうとしていることを示しています。あなたはエラーが出るん

class Knoten 
{ 
    // ... 

    public static implicit operator Point(Knoten source) 
    { 
     return new Point(source.X, source.Y); 
    } 
} 
関連する問題