C#에서 Python's getattr()과 같은 것이 있습니까? 창에 넣을 컨트롤 이름이 들어있는 목록을 읽음으로써 창을 만들고 싶습니다.C#에서 파이썬의 getattr()과 같은 것이 있습니까?
13
A
답변
9
Type.InvokeMember도 있습니다.
public static class ReflectionExt
{
public static object GetAttr(this object obj, string name)
{
Type type = obj.GetType();
BindingFlags flags = BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.GetProperty;
return type.InvokeMember(name, flags, Type.DefaultBinder, obj, null);
}
}
처럼 사용될 수
(확장 법 등)
object value = ReflectionExt.GetAttr(obj, "PropertyName");
또는
:
object value = obj.GetAttr("PropertyName");
3
이에 대한 반성을 사용하십시오.
Type.GetProperty()
및 Type.GetProperties()
각각은 객체의 속성 값을 읽는 데 사용할 수있는 PropertyInfo
인스턴스를 반환합니다.
var result = typeof(DateTime).GetProperty("Year").GetValue(dt, null)
Type.GetMethod()
Type.GetMethods()
및 각 객체의 메소드를 실행하는데 사용될 수 MethodInfo
인스턴스를 반환한다.
var result = typeof(DateTime).GetMethod("ToLongDateString").Invoke(dt, null);
당신은 반드시 여러분이이 같은 일을 할 수있는 것보다, (당신은 속성 이름을 새 경우 조금 이상한 것) 유형을 모르는 경우.
var result = dt.GetType().GetProperty("Year").Invoke(dt, null);
1
예, 당신이 할 수있는 ...
typeof(YourObjectType).GetProperty("PropertyName").GetValue(instanceObjectToGetPropFrom, null);
0
object.GetType를 사용하여 생성 할 수 System.Reflection.PropertyInfo 클래스가있다(). GetProperties를(). 이를 사용하여 문자열을 사용하여 객체의 속성을 검사 할 수 있습니다. (객체 메서드, 필드 등에 대해서도 비슷한 메서드가 있습니다.)
당신의 목표를 달성하는 데 도움이 될 것이라고 생각하지 않습니다. 아마도 객체를 직접 생성하고 조작해야합니다. 컨트롤에는 예를 들어 설정할 수있는 Name 속성이 있습니다.
관련 문제
- 1. NSBottomBar와 같은 것이 있습니까?
- 2. Server.FileNameEncode와 같은 것이 있습니까?
- 3. CURLOPT_CONNECTIONTIMEOUT과 같은 것이 있습니까?
- 4. ActionScript 2.0에서 C의 #define과 같은 것이 있습니까?
- 5. PHP에는 파이썬의 템플릿 문자열과 같은 기능이 있습니까?
- 6. Objective C에는 parse_url과 같은 것이 있습니까?
- 7. Ruby 용 Python의 pty.fork와 같은 것이 있습니까?
- 8. MATLAB에서 'whereis'와 같은 것이 있습니까?
- 9. jQuery에는 isClicked와 같은 것이 있습니까?
- 10. ActionMailer에 필터와 같은 것이 있습니까?
- 11. PHP에서 Chronic과 같은 것이 있습니까?
- 12. 기능 모델과 같은 것이 있습니까?
- 13. Jsp 다이어그램과 같은 것이 있습니까?
- 14. 웹용 TextMate 같은 것이 있습니까?
- 15. Readability.js와 같은 Python을위한 것이 있습니까?
- 16. XML 2.0과 같은 것이 있습니까?
- 17. Linux의 Shark와 같은 것이 있습니까?
- 18. 파이썬의 httplib.HTTPConnection 블록이 있습니까?
- 19. HTTP :: Recorder for Python과 같은 것이 있습니까?
- 20. Linux 용 WMI와 같은 것이 있습니까?
- 21. iPad에서 실행되는 squeak etoys과 같은 것이 있습니까?
- 22. java.awt.Graphics의 "Anchors"(javax.microedition.lcdui.Graphics)와 같은 것이 있습니까?
- 23. 그래프 데이터베이스에 스키마 같은 것이 있습니까?
- 24. ActiveRecord (Rails)의 create_view와 같은 것이 있습니까?
- 25. 컨트롤러에 대한 살균 기능과 같은 것이 있습니까?
- 26. PHP 용 Acegi와 같은 것이 있습니까?
- 27. 팟 캐스트 용 IMAP과 같은 것이 있습니까?
- 28. Java와 JUnit을위한 ZenTest/Autotest와 같은 것이 있습니까?
- 29. .NET Remoting을위한 OnClientConnected 이벤트와 같은 것이 있습니까?
- 30. Action Script 3에 printf와 같은 것이 있습니까?