下面是图的深度优先遍历递归算法算法,请完成。

动态添加属性到的ExpandoObject-c#,c#-4.0,dynamic,expandoobject-CodeGo.net
动态添加属性到的ExpandoObject
我想动态的的ExpandoObject添加属性时因此,例如,添加一个字符串属性调用NewProp我想写出像
var x = new ExpandoObject();
x.AddProperty("NewProp", System.String);
这是很容易可能吗?
本文地址 :CodeGo.net/254418/
-------------------------------------------------------------------------------------------------------------------------
1. dynamic x = new ExpandoObject();
x.NewProp = string.E
var x = new ExpandoObject() as IDictionary&string, Object&;
x.Add("NewProp", string.Empty);
看到源代码。
public class DynamicObj : DynamicObject, IDictionary&string, object&
// Wrap an internal dictionary
// Override DynamicObject's TryGetProperty(), TrySetProperty()
DynamicObj d = new DynamicObj();
d["Pa"] = 1;
d.Add("Pb", 2);
int i = d.Pa;
int j = d.Pb;
int k = d.Pc;
// That is it..
本文标题 :动态添加属性到的ExpandoObject
本文地址 :CodeGo.net/254418/
Copyright (C) 2017 CodeGo.netc# 编程(255)
ExpandoObject :动态的增删一个对象的属性,在低层库(例如ORM)中非常有用。由于ExpandoObject实现了IDictionay&string, object&接口,常见的一种用法是,把expando转成dictionary,动态增加属性名和值[key,value],expando就达到了动态属性的目的。 &示例代码(using System.Dynamic): dynamic expando = new ExpandoObject();
expando.A = &a&;
expando.B = 1;
IDictionary&string, object& dict = expando as IDictionary&string, object&;
foreach(var e in dict){
Console.WriteLine(e.Key + &,&+ e.Value);
dict.Add(&C&, &c&);
Console.WriteLine(expando.C);DynamicObject 类:当需要track一个对象的属性被get或set时,这个类是很好的候选。 (通常出现在AOP的设计中,如果不打算使用AOP框架例如POSTSHARP的话),示例代码:void Main()
dynamic obj = new MyDynaObject();
obj.A = &a&;
obj.B = 1;
var a = obj.A;
// should print :
// tring to set member : A, with value : a
// tring to set member : B, with value : 1
// tring to get member : a
public class MyDynaObject : DynamicObject
// The inner dictionary.
Dictionary&string, object& dictionary
= new Dictionary&string, object&();
// This property returns the number of elements
// in the inner dictionary.
public int Count
return dictionary.C
// If you try to get a value of a property
// not defined in the class, this method is called.
public override bool TryGetMember(
GetMemberBinder binder, out object result)
// Converting the property name to lowercase
// so that property names become case-insensitive.
string name = binder.Name.ToLower();
Console.WriteLine(&tring to get member : {0}&, name);
// If the property name is found in a dictionary,
// set the result parameter to the property value and return true.
// Otherwise, return false.
return dictionary.TryGetValue(name, out result);
// If you try to set a value of a property that is
// not defined in the class, this method is called.
public override bool TrySetMember(
SetMemberBinder binder, object value)
// Converting the property name to lowercase
// so that property names become case-insensitive.
dictionary[binder.Name.ToLower()] =
Console.WriteLine(&tring to set member : {0}, with value : {1}&, binder.Name, value);
// You can always add a value to a dictionary,
// so this method always returns true.
// Define other methods and classes
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1062023次
积分:19939
积分:19939
排名:第346名
原创:848篇
转载:88篇
评论:166条
(9)(8)(21)(16)(7)(8)(8)(10)(9)(8)(4)(10)(9)(9)(5)(6)(44)(52)(55)(36)(19)(14)(16)(10)(13)(9)(7)(8)(6)(11)(18)(11)(6)(8)(19)(11)(19)(18)(5)(10)(13)(9)(9)(12)(16)(12)(6)(7)(6)(6)(14)(5)(5)(13)(3)(2)(5)(17)(22)(30)(34)(12)(11)(4)(9)(18)(7)(27)(23)(5)(2)(9)(1)一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。
本文标题:
本页链接:

我要回帖

更多关于 图的广度优先遍历算法 的文章

 

随机推荐