2017-09-20 15 views
0

에 대한 찾을 수 없습니다 : Typeconverter not working for single propertiesTypeConverter를이 기본적으로 질문의 연속이다 재산

원래의 질문에 대한 답변이 표시된되지 않은, 그러나 그것은 모든 때문에 특히 내 프로젝트에서 특정 시나리오에 대한 앞으로의 방법이 될 듯 제 독서는 그것이 정말로 효과가 있어야한다고 제안합니다. 나는 한 번 슛을 주었지만 마지막 코멘트에서 언급 한 것과 같은 문제가 발생했다. (브레이크 포인트가 맞지 않거나 나의 경우 출력 결과 창에 아무런 출력이 없다.) 여기에 내가 시작하는 것이 있는데, 나의 필요를위한 기본 예제에서 †. LINQPad에서

는 출력이 문제가 1 일)는 TypeConverterAttribute 정의가 표시되지이다 제안

typeof(IEnumerable<String>) 
System.Collections.Generic.IEnumerable`1[System.String] 

ReferenceConverter 
System.ComponentModel.ReferenceConverter 

null 

void Main() 
{ 
    var prop = typeof(Request).GetProperty("Names"); 
    var converter = TypeDescriptor.GetConverter(prop.PropertyType.Dump()).Dump(); 
    var value = converter.ConvertFrom("One,Two").Dump(); 
} 

public class Request 
{ 
    [TypeConverter(typeof(EnumerableTypeConverter<string>))] 
    public IEnumerable<string> Names { get; set; } 
} 

public class EnumerableTypeConverter<T> : TypeConverter 
{ 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     new { Context = context, SourceType = sourceType }.Dump("CanConvertFrom"); 

     if (sourceType == typeof(string)) 
      return true; 

     return false; 
    } 

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     new { Context = context, DestinationType = destinationType }.Dump("CanConvertTo"); 

     return true; 
    } 

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
    { 
     new { Context = context, Culture = culture, Value = value }.Dump("ConvertFrom"); 

     var source = value as string; 

     if (source == null) 
      return value.ToString(); 

     return source; 
    } 

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
    { 
     new { Context = context, Culture = culture, Value = value, DestinationType = destinationType }.Dump("ConvertTo"); 

     var s = value as IEnumerable<string>; 

     return s != null ? string.Join(",", s) : base.ConvertFrom(context, culture, value); 
    } 

    public override bool IsValid(ITypeDescriptorContext context, object value) 
    { 
     new { Context = context, Value = value }.Dump("IsValid"); 

     return true; 
    } 
} 

입니다

, 2) 내 정의를 찾는되지 않는다 TypeConverter 또는 3) 생성 할 수 없으며 자동으로 실패합니다. 그들 모두는있을 법하지 않지만 분명히 결과는 거짓말하지 않습니다. 따라서 내 이해에서 무언가 저조합니다. 인스턴스를 직접 인스턴스화 할 때 예외가 발생하지 않았습니다.

var testConverter = new EnumerableTypeConverter<string>().Dump(); 

EnumerableTypeConverter<String> 
UserQuery+EnumerableTypeConverter`1[System.String] 

는 싱긋 웃음과 웃음, 나는 비 일반적인 접근 방식

[TypeConverter(typeof(EnumerableStringConverter))] 
public IEnumerable<string> Names { get; set; } 

public class EnumerableStringConverter : TypeConverter 
{ 
    // -- same implementation as EnumerableTypeConverter<T> -- 
} 

을 시도했지만 같은 결과를 받았다. 내가 여기서 무엇을 놓치고 있니?


† 코드는 속성 이름 및 관련 값을 포함하는 파일을 읽습니다. 다음으로 이름으로 일치하는 속성을 찾고 변환기를 통해 값을 설정합니다. 실제 값 중 일부는 JSON 배열이지만, deserialize 된 JSON 배열을 일치하는 속성으로 반환하는 전체 솔루션을 만들기 전에 내가했던 일을 검증하기위한 작은 단계를 거쳤습니다.

답변

2

변환기를 속성으로 설정하면 IEnumerable<string>이 아닌이 속성의 변환기를 가져와야합니다.

static void Main(string[] args) 
{ 
    var properties = TypeDescriptor.GetProperties(typeof(Request)); 
    var propItem = properties["Names"]; 
    var converter = propItem.Converter; 
    // converter has type {EnumerableTypeConverter<string>} 
}