How to make C # programming like JavaScript

Source: Internet
Author: User

JavaScript is a dynamic language that can dynamically add properties and methods to objects, which is very convenient. Is there a way for C # to have the ability to dynamically add properties and methods, programming like JavaScript? Here is a good framework claysharp can achieve the above functions.

After building a new object with Claysharp, the following code can be used to dynamically add properties using new:

Here is a detailed step:

1 Create a new console program and introduce the class library

You must introduce the Claysharp namespace:

1 // Claysharp enables C # to have dynamic programming capabilities for JavaScript 2 using Claysharp;

Create a new object with Clayfactory, and you can create a dynamic object at random after the new object:

1 DynamicNew =Newclayfactory ();2 varperson =New.person ();3person["FirstName"] ="Louis";4person["LastName"] ="Dejardin";5Person. Age = -;

Object properties can be created by using [] or. You can also create a dynamic method:

1 varMan =New.jackwang ();2Man. FirstName ="Jack";3Man. LastName ="Wang";4Man. Sayfullname =Newfunc<string,string> (x = man. FirstName + man. LastName +x);5Output6Console.WriteLine (man. Sayfullname () ("here!"));

Claysharp also supports the creation and assignment of attributes using concatenating methods:

1 varPeoples =New.array (2New.person (). FirstName ("Jack"). LastName ("Wang"),3New.person (). FirstName ("Jack"). LastName ("CUMT")4 );5Console.WriteLine ("{0}","----------Claysharp-------------");6 foreach(varPinchpeoples)7 {8Console.WriteLine ("{0} {1}", P.firstname, p.lastname);9}

In fact, in the higher version of C #, there are built-in object ExpandoObject also have similar functions, but the function is slightly weaker than Claysharp:

1Console.WriteLine ("{0}","-----System.Dynamic.ExpandoObject----");2 //---------------ExpandoObject---------------------------------------------3 DynamicExpobject =NewSystem.Dynamic.ExpandoObject ();4Expobject.nickname ="Jack";5 //expobject["nickname"] = "Jack";//Error6Expobject.realname ="Wang";7Console.WriteLine ("{0} {1}", Expobject.nickname, expobject.realname);8Console.ReadLine ();

How to make C # programming like JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.