Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 think this 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 something to a large string, which adds to the equation
And then I eval the string
Is there another way to do this?
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/program$$anonymous$$g/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 achieve something 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 within your game / application.

It is highly 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 which 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 things the common solution is having a backend with Node / PHP or something 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.

Follow this Question

Answers Answers and Comments

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

Related Questions

Which language should I start learning for scripting? 1 Answer

Using PlayerPrefs to transfer variables from Javascript to C# 1 Answer

Distribute terrain in zones 3 Answers

Changing Value in JavaScript from C# 1 Answer

Trying to figure out how to translate these gravity script to Javascript 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