티스토리 뷰
문자열로 Type 오브젝트 인스턴스 시키기 (Instantiate a System.Type from a type definition string)
kaki104 2014. 4. 15. 21:55참고
Instantiate a System.Type from a type definition string
http://stackoverflow.com/questions/13842173/instantiate-a-system-type-from-a-type-definition-string
앱을 개발하기 위해 CrossPlatform이라는 라이브러리 프로젝트를 사용하는데, 이런 경우 스트링으로 인스턴스를 시켜야 하는 경우가 많이 있다. 일반적으로는
Type t = Type.GetType(typeName, false);
object result = null;
if(t != null) result = Activator.CreateInstance(t);
위의 문장으로 처리가 되지만, 이 문장이 동작하기 위한 선결 조건이 사용하는 프로젝트 내부에 해당 Type이 있어야 한다는 것이다. 만약 클래스라이브러리에서 위의 문장을 실행하면 Type을 찾을 수 없게 된다.
그런 경우 아래와 같이 하면 Type을 찾고 인스턴스 시킬 수 있다.
* contentTypeName은 네임스페이스까지 포함한 Type의 FullName이다.
Popup popup = new Popup();
var contentType = Application.Current.GetType().GetTypeInfo().Assembly.GetType(contentTypeName);
if (contentType != null)
{
var content = Activator.CreateInstance(contentType);
if (content != null && content is UIElement)
{
popup.Child = (UIElement)content;
}
}
위와 같이 하면, 실행 중인 앱의 어셈블리에서 Type을 찾아서 반환한다.
'Previous Platforms' 카테고리의 다른 글
Screenshot AutoCut Privacy Policy (0) | 2014.04.17 |
---|---|
Screenshot AutoCut Help (0) | 2014.04.17 |
Windows 8.1 tips (2) | 2013.12.06 |
Windows Live Connect Manage (0) | 2013.12.06 |
Nokia Lumia 925 Get! (0) | 2013.12.03 |
- Total
- Today
- Yesterday
- ComboBox
- .net 5.0
- dotNETconf
- Build 2016
- uno-platform
- kiosk
- C#
- #uwp
- uno platform
- Windows 10
- Microsoft
- Behavior
- UWP
- IOT
- windows 11
- Cross-platform
- .net
- Bot Framework
- #prism
- #MVVM
- #Windows Template Studio
- WPF
- LINQ
- ef core
- visual studio 2019
- Visual Studio 2022
- XAML
- PRISM
- Always Encrypted
- MVVM
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |