• 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 ei1 · Sep 21, 2015 at 12:15 AM · c#javascriptjavascript-specificc# to javascripteval

Is there eval() for C#?

I'm making a calculator and I use UnityScript already but I'm just curious if eval() is in C#
I t$$anonymous$$nk t$$anonymous$$s would also help others that want to use eval()

If there is a way around every scenario that uses eval, here's how my calculator would work:
User types in equation and its received as a string,
or buttons that would add somet$$anonymous$$ng to a large string, w$$anonymous$$ch adds to the equation
And then I eval the string
Is there another way to do t$$anonymous$$s?
Even so, is there an eval in C#? It would help other possible scenarios.

Comment
Add comment · Show 1
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 maccabbe · Sep 21, 2015 at 01:34 AM 0
Share

http://www.ckode.dk/programming/eval-in-c-yes-its-possible/

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Sep 21, 2015 at 02:31 AM

The short answer is: No, there is no eval function implemented in C#. However it's possible to ac$$anonymous$$eve somet$$anonymous$$ng similar.

A few more details:

Using eval is in general a great security risk, especially when the eval string is created by the user. He can literally execute any code wit$$anonymous$$n your game / application.

It is $$anonymous$$ghly recommended to never use eval at all if possible. That's also true for (web) Javascript code.

If you just want to evaluate maths expressions you can use a simple expression parser like the one i've written w$$anonymous$$ch i've posted on the wiki over here. It's written 100% in managed code and hasn't any special dependencies. I've posted a more detailed answer over here where i included a webplayer example. The parser itself can be extended with custom functions / constants / values with just a single line.

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 Dreamora · Sep 21, 2015 at 07:39 AM

There is no eval on c#. There are multiple reasons (like the compiler is not present on the client etc) but the biggest one is that eval is not cross platform usable as all Ahead of Time (AoT) compilation platforms (iOS, consoles) do explicitely not allow generation of code at runtime for security reasons so eval will actually fail on these platforms.

Bunny83 solution is one that works if its just about maths, if you want to do more complicated t$$anonymous$$ngs the common solution is having a backend with Node / PHP or somet$$anonymous$$ng else thats dynamic and send a string there to evaluate, getting back the desired result.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Lower player's health from separate script. 2 Answers

Having a problem translating part of a script which uses C# delegates into JS 0 Answers

Eval or Can I call JScript from an Editor C# Script? 1 Answer

Convert c# to Js? 1 Answer

Multiple Cars not working 1 Answer


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