.net 4.0 有DLR支持部分动态。比如:dynamic 关键字, ExpendoObject 对象等。 http://msdn.microsoft.com/zh-cn/library/system.dynamic.expandoobject.aspx class Program { static void Main(string[] args) { dynamic employee, manager; employee = new ExpandoObject(); employee.Name = "John Smith"; employee.Age = 33; manager = new ExpandoObject(); manager.Name = "Allison Brown"; manager.Age = 42; manager.TeamSize = 10; WritePerson(manager); WritePerson(employee); } private static void WritePerson(dynamic person) { Console.WriteLine("{0} is {1} years old.", person.Name, person.Age); // The following statement causes an exception // if you pass the employee object. // Console.WriteLine("Manages {0} people", person.TeamSize); } } // This code example produces the following output: // John Smith is 33 years old. // Allison Brown is 42 years old.
当然你可以: var People=new {Name="me",Year=1988}; var Student=new {Name="菜鸟me",Course="数学",Score=99}; 如此而已... 至于删除还真不知道..你可以自己搞个集合,创建对象使用删除...随意 .javascript是解释型语言,我个人认为啊,比如继承方面是采用property原型扩展,要什么直接在一个对象上加就是,相当随意;而不是像C#那样所有的对象都是继承自Object,在对象的定义操作上都时刻以安全为主,需要经过C#编译器,JIT编译器,运行时的层层检验与核实.
上面的代码可以这么用: var Student = new { Name = "菜鸟me", Course = "数学", Score = 99 }; //Name:菜鸟me Course:数学,Score:99 Console.WriteLine("Name:{0} Course:{1},Score:{2}", Student.Name, Student.Course, Student.Score);
http://msdn.microsoft.com/zh-cn/library/system.dynamic.expandoobject.aspx
class Program
{
static void Main(string[] args)
{
dynamic employee, manager; employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33; manager = new ExpandoObject();
manager.Name = "Allison Brown";
manager.Age = 42;
manager.TeamSize = 10; WritePerson(manager);
WritePerson(employee);
}
private static void WritePerson(dynamic person)
{
Console.WriteLine("{0} is {1} years old.",
person.Name, person.Age);
// The following statement causes an exception
// if you pass the employee object.
// Console.WriteLine("Manages {0} people", person.TeamSize);
}
}
// This code example produces the following output:
// John Smith is 33 years old.
// Allison Brown is 42 years old.
var People=new {Name="me",Year=1988};
var Student=new {Name="菜鸟me",Course="数学",Score=99};
如此而已...
至于删除还真不知道..你可以自己搞个集合,创建对象使用删除...随意 .javascript是解释型语言,我个人认为啊,比如继承方面是采用property原型扩展,要什么直接在一个对象上加就是,相当随意;而不是像C#那样所有的对象都是继承自Object,在对象的定义操作上都时刻以安全为主,需要经过C#编译器,JIT编译器,运行时的层层检验与核实.
//Name:菜鸟me Course:数学,Score:99
Console.WriteLine("Name:{0} Course:{1},Score:{2}", Student.Name, Student.Course, Student.Score);
var Student = new { Name = "菜鸟me", Course = "数学", Score = 99, Method = action };
//Name:菜鸟me Course:数学,Score:99,MethodExecute:CAINIAOME
Console.WriteLine("Name:{0} Course:{1},Score:{2},MethodExecute:{3}", Student.Name, Student.Course, Student.Score, Student.Method("cainiaome"));
它的主要是NET4.0,我目前4.0主要是拿来测试,很多人还没用4.0所以想从低于4.0版本尝试下。
还是第一次见到,原来C# 还可以这样来写。
# 6 7 楼 xx_mm 的写法:
我想了一下,试着写,不过还是写不出来。
以前我的想法:
把 属性,都压进字典里去的。
如下写法:----每来一个对象!add 一次!删就用remove,
Hashtable table = new Hashtable();
table.Add("key","value");
对于方法, xx_mm 说用 委托。 貌似 委托这个东东,在声明时,它的返回参数都已经确定了,
真的不知如何构思。
还望 大家 能 细详 地说一说。
var Student = new { Name = "菜鸟me", Course = "数学", Score = 99, Method = action };
//Name:菜鸟me Course:数学,Score:99,MethodExecute:CAINIAOME
Console.WriteLine("Name:{0} Course:{1},Score:{2},MethodExecute:{3}", Student.Name, Student.Course, Student.Score, Student.Method("cainiaome"));
var fun = function(_) { return _.toString().toUpperCase(); };
var Student = { Name: "菜鸟me",Course: "数学",Score: 99,Method: fun };
////Name:菜鸟me Course:数学,Score:99,MethodExecute:CAINIAOME
document.write("Name:" + Student.Name + " Course:" + Student.Course + ",Score:" + Student.Score + ",Method:" + Student.Method("cainiaome"));
那么C# 有个种字典?
Hashtable ExpendoObject 除外。
属性:item.add("name",value);
无返回值方法:item.add("name",new Action(……));
有返回值方法:item.add("name",new Func<returnValue>(……));
基本跟javascript一个思路。