반응형
문자열을 모든 유형으로 변환하는 방법
문자열을 일반 유형으로 변환하고 싶습니다.
나는 이것을 가지고있다:
string inputValue = myTxtBox.Text;
PropertyInfo propInfo = typeof(MyClass).GetProperty(myPropertyName);
Type propType = propInfo.PropertyType;
object propValue = ?????
'inputString'을 해당 속성의 유형으로 변환하여 호환되는지 확인하려면 어떻게해야합니까?
tks
using System.ComponentModel;
TypeConverter typeConverter = TypeDescriptor.GetConverter(propType);
object propValue = typeConverter.ConvertFromString(inputValue);
object propvalue = Convert.ChangeType(inputValue, propType);
뭘 보관하려고하는지 잘 모르겠지만 .. 다이내믹 캐스팅을 말하는 건가요? 이 같은:
TypeDescriptor.GetConverter(typeof(String)).ConvertTo(myObject, typeof(Program));
건배.
참조 URL : https://stackoverflow.com/questions/2922855/how-to-convert-string-to-any-type
반응형
'Development Tip' 카테고리의 다른 글
Ruby 1.9를 Ubuntu에서 기본 Ruby로 만들려면 어떻게해야합니까? (0) | 2020.12.15 |
---|---|
ArrayList를 읽기 전용으로 설정 (0) | 2020.12.15 |
Visual Studio 2010은 예상대로 종속성 인 프로젝트에서 정적 라이브러리를 자동 연결하지 않습니다. (0) | 2020.12.15 |
iOS에서 SHA-2 (이상적으로 SHA 256 또는 SHA 512) 해시를 계산하려면 어떻게해야합니까? (0) | 2020.12.15 |
한 서비스의 로그를 별도의 파일에 작성하는 방법은 무엇입니까? (0) | 2020.12.15 |