회원 로그인
|
object 형 변환 (with Unity)
object 형 변환
json 파일을 파싱하려는 중 Dictionary<string, object> 형으로 받아 왔다.
object를 int, float 형 변환
구글링하여 찾아보았다.
obejct를 Hashtable(해시테이블)로 바꿔서 형 변환을 하라, Convert.ChangeType() 함수로 형 변환을 하라는 등... 점점 어려운 말만 되어 있다.
object를 enum 형 변환
이 기세로 value 값을 enum으로 변환할 차례이다.
다시 구글링..
쉽게 변환을 할 수 있었다.
더 찾아보니 int나 float형 변환은...
object를 array 형 변환
이번에는 배열형으로 형변환 해봐야 한다.
의외로 싶게 찾았고 적용해 보았다.
두꺼운 책을 사서 정독해야 한다지만 현재로는 시간이 없어 무작정 배껴 수정하며 사용해야 한다;;;
json 파일을 파싱하려는 중 Dictionary<string, object> 형으로 받아 왔다.
// json 형태 { "1": { "type": "Easy", "id": "", "name": "", "document": "", "level": 1, "maxLevel": 3, "experience": 1, "moveSpeed": 1.0, "strength": 1.0, "attackSpeed": 1.0, "hp": 10, "maxAp": 10, "maxSp": 10, "skills": [ "AS0", "PS0" ], "dropItems": [ "" ], "dropGold": 10, "dropGem": 0, "spriteRenderer": [ "" ], "textures": [ "" ] } }object형인 value값이 int, float, string, array 등의 형태를 가지고 있기 때문에 object 타입을 형 변환하여 사용해야 하는데 문제가 발생, C#을 깊게 공부하지 않아 할 줄 모른다는 거다!!
// 단순 형 변환 Dictionary<string, object> objDic; int value = (int)objDic["key"];단순한 형 변환을 하였는데... 유니티에서 InvalidCastException 에러
object를 int, float 형 변환
구글링하여 찾아보았다.
obejct를 Hashtable(해시테이블)로 바꿔서 형 변환을 하라, Convert.ChangeType() 함수로 형 변환을 하라는 등... 점점 어려운 말만 되어 있다.
// Convert.ChangeType() 사용한 예 int value = (int)Convert.ChangeType(objDic["key"], typeof(int));형 변환이 잘 된다.
object를 enum 형 변환
이 기세로 value 값을 enum으로 변환할 차례이다.
다시 구글링..
public EnumType enumType = new EnumType(); void main() { // enumType = enumParse<EnumType>(objDic["key"]); } // enum 파싱 public T enumParse<T>(object obj) { T enumVal = (T)Enum.Parse(typeof(T), obj.ToString()); return enumVal; } public enum EnumType { Easy, Normal, Hard }Generic을 사용하여 EnumType의 value의 string 값으로 파싱하게 되어 있다.
쉽게 변환을 할 수 있었다.
더 찾아보니 int나 float형 변환은...
int value = int.Parse(objDic["key"].ToString());이렇게 쉽게도 캐스팅이 가능했다.;;;
object를 array 형 변환
이번에는 배열형으로 형변환 해봐야 한다.
의외로 싶게 찾았고 적용해 보았다.
using System.Linq; ToStringArray(objDic["key"]); static string[] ToStringArray(object obj) { var collection = obj as IEnumerable; if (collection != null) { return collection.Cast<object>().Select(x => x.ToString()).ToArray(); } if (obj == null) { return new string[] { }; } return new string[] { obj.ToString() }; }
두꺼운 책을 사서 정독해야 한다지만 현재로는 시간이 없어 무작정 배껴 수정하며 사용해야 한다;;;
댓글 1개
| 엮인글 0개
285개(5/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
205 | [Python] Anaconda 사용 | 푸딩뱃살 | 3412 | 2017.08.28 17:51 |
204 | [Python] url 인코딩/디코딩 | 푸딩뱃살 | 6312 | 2017.08.12 19:23 |
203 | [Python] Qt5(ui) to py | 푸딩뱃살 | 2622 | 2017.07.04 15:29 |
202 | [VisualStudio] Visual Studio Community 2015 설치 파일 (다운로드) [1+1] | 푸딩뱃살 | 4497 | 2017.06.11 20:11 |
201 | [VisualStudio] Visual Studio Community 2015 삭제 | 푸딩뱃살 | 4270 | 2017.06.11 20:06 |
200 | [Python] PyQt4 to PyQt5 | 푸딩뱃살 | 2968 | 2017.06.02 16:50 |
199 | [Python] NumPy 라이브러리 설치 | 푸딩뱃살 | 3342 | 2017.05.18 15:14 |
198 | [Python] Python Decompiler | 푸딩뱃살 | 3934 | 2017.04.28 14:28 |
197 | [Python] 암호화 ASE | 푸딩뱃살 | 3426 | 2017.01.09 00:10 |
196 | [Python] 모듈 with xbox 360 controller | 푸딩뱃살 | 2097 | 2016.09.18 22:03 |
195 | [PHP] php 메모리 부족 (PHP Fatal error) | 푸딩뱃살 | 4300 | 2016.09.10 21:07 |
194 | [C#] 조건연산자 ?: | 푸딩뱃살 | 2937 | 2016.09.06 13:46 |
>> | [C#] object 형 변환 (with Unity) [1+1] | 푸딩뱃살 | 10530 | 2016.05.04 02:20 |
192 | [C#] 현재 시간 | 푸딩뱃살 | 3029 | 2016.02.28 23:23 |
191 | [C#] 문자열 Format | 푸딩뱃살 | 2461 | 2016.01.21 12:10 |
190 | [XML] XML 파싱 (with Unity) | 푸딩뱃살 | 7032 | 2016.01.20 10:33 |
189 | [PHP] PHP 전송 종류 | 푸딩뱃살 | 2404 | 2016.01.11 10:49 |
188 | [C#] 배열 | 푸딩뱃살 | 1199 | 2016.01.05 14:38 |
187 | [C#] 참조 ref, out (with Unity) | 푸딩뱃살 | 6068 | 2015.12.08 11:22 |
186 | [PHP] CodeIgniter(코드이그나이트) 사용 | 푸딩뱃살 | 11874 | 2015.12.06 17:46 |