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 Thet Naing Swe · Aug 23, 2010 at 07:27 AM · typecastdynamic typing

C# dynamic typing

Can C# script use the dynamic typing like in Javascript? Does C# contain similar functionality like the Javascript?

Javascript

var objectType = typeof(object); if (objectType == GUITexture) { //Do Something }

so.. what will be the C# code for the above Javascript?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Wolfram · Aug 23, 2010 at 11:49 AM

Actually that's quite simple, but it is hard to find the correct documentation pages on that:

if(object is GUITexture){
    // Do Something
}

This also works with derived classes (up and down). You might also want to check the docs for GetType() and IsSubclassOf(), if you are working with derived classes. But the "is" keyword simplifies things. The actual dynamic cast would be:

GUITexture myTexture=object as GUITexture;

Avoid static casts ( myTexture=(GUITexture)obj ), as they will throw an exception and not compiler errors for derived classes.

EDIT: So, no there is no "real" dynamic typing / duck typing in (Unity-)C#. But with "is" and "as" you are still quite flexible, and the programmer himself can make sure that what he is trying to do is actually allowed (such as, casting from A to B).

Comment
Add comment · 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
0

Answer by cyangamer 1 · Aug 23, 2010 at 10:14 AM

Perhaps you can view this link? There is a 'dynamic' type in C#, and it sounds like that's what you want.

Comment
Add comment · Show 3 · 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 Wolfram · Aug 23, 2010 at 11:23 AM 0
Share

That link contains some useful information, I had been looking for that. However, I'm not sure the 'dynamic' type is supported in Unity. Have you tried it?

avatar image Hibame · Oct 14, 2010 at 11:01 AM 0
Share

If I am reading http://unity3d.com/unity/whats-new/unity-3 right and we now have the mono version of C# 3.5 we can indeed do that. Why one would want to do that I can't quite catch on as it can only be used in the local scope.

avatar image kabel · Aug 15, 2011 at 10:31 PM 0
Share

the dynamic keyword does not work. i use this (http://www.unifycommunity.com/wiki/index.php?title=JSONObject) json parser ins$$anonymous$$d. dynamic progra$$anonymous$$g ftw!

avatar image
0

Answer by AnomalusUndrdog · Aug 01, 2012 at 11:17 AM

Since the dynamic keyword doesn't work, the closest you could do is make interfaces.

http://lmgtfy.com/?q=c%23+interface+tutorials

As for the C# equivalent of:

 var objectType = typeof(object); if (objectType == GUITexture) { //Do Something }

It would be:

 var objectType = object.GetType(); if (objectType == typeof(GUITexture)) {  }
Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Accessing properties from variable 1 Answer

Cast Static variables as generic ones? 1 Answer

Classes and type casting? 2 Answers

"Cannot cast from source type to destination type"- instantiating 4 Answers

Cannot cast from source type to destination type 2 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