Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kannan21 · Jul 12, 2014 at 12:36 PM · reflectiontypecastingtypecastinggeneric list

Problem with type casting?

 The following is my extension method to cast types 
 
 public static T Cast<T>(this object obj)
 {
     return (T)obj;
 }
 
 This is my extension to convert list to array:
 
 public static List<Transform> ToTransformList(this List<WaypointData> waypointsDataList)
     {
         List<Transform> waypoints = new List<Transform>();
         foreach(WaypointData wd in waypointsDataList)
         {
             waypoints.Add(wd.waypoint);
         }
         return waypoints;
     }
 
 I use reflection and in that if i find any filled info of type list i need to convert it to array. The filed info
 
 FieldInfo[] fieldinfos = kamal.GetType ().GetFields();
         foreach(FieldInfo field in fieldinfos)
         try{
             if(field.FieldType.GetGenericTypeDefinition()==typeof(List<>))
             {
                                 //kamal actual type of List<String>//    
                     var cast = field.GetValue(kamal);
                                //here i need to make the program understand that this is some kind of list. Only then i can use my ToArray extension method. But the following is not working.
                    var array = cast.Cast<cast.GetType()>().ToArray();
             }
         }
         catch{}


Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wisearn · Jul 12, 2014 at 12:46 PM 0
Share

If you don't $$anonymous$$d, please clarify what the problem is and your errors in cleartext outside of the code format.

avatar image kannan21 · Jul 12, 2014 at 12:56 PM 0
Share

var array = cast.Cast().ToArray();

in the above line i cannot pass the type i want to cast to using get type method. It give me the following error.

Assets/DataContainer.cs(39,71): error CS1525: Unexpected symbol `)'

I need to type cast generically. How to do it?

Thanks.

avatar image vexe · Jul 12, 2014 at 01:09 PM 0
Share

Why did you write a method that converts an to a list? - There's already a ToList method in Enumerable (just add using System.Linq)

avatar image kannan21 · Jul 12, 2014 at 01:13 PM 0
Share

You are right i simply did. I can use the one in System.Linq the only prolem i'm having here is with type casting.

avatar image vexe · Jul 12, 2014 at 01:54 PM 0
Share

@kannan21 it seems to me you want to cast your kamal string list to an array? you can simply do this by (it should work):

 var list = field.GetValue(kamal) as IEnumerable;
 var array = list.ToArray(); // not your ToArray, but the one in System.Linq

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Tehnique · Jul 12, 2014 at 01:53 PM

Don't think you can't resolve your generics at runtime, as you are trying to do with GetType(). Anyway, here are some pointers that could help.

MakeGenericType will also work with open generic types (as is your case) as stated here.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kannan21 · Jul 15, 2014 at 11:09 AM 0
Share

Awesome :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I use a variable with getcomponent<>()? 2 Answers

Make compiler treat an object variable as if it was of type X 2 Answers

GameObject.Find() type casting question 1 Answer

A smarter Way to get the Type of SerializedProperty 6 Answers

I need an Array of System.type[] from all of my ScriptableObjects scripts 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges