private DynamicInstance GetPropertyValue(DynamicInstance depProperty) { int packedData = depProperty.AsDynamic._packedData; var globalIndex = (short)(packedData & 0xFFFF); var effectiveValue = _effectiveValues.FirstOrDefault(ev => (short)ev.AsDynamic._propertyIndex == globalIndex); if (effectiveValue != null) { DynamicInstance internalValue = effectiveValue.AsDynamic._value; if (internalValue.Type.Name == ModifiedValueTypeName) { short fullValueSource = effectiveValue.AsDynamic._source; if (IsCoerced(fullValueSource)) { return(internalValue.AsDynamic._coercedValue); } if (IsAnimated(fullValueSource)) { return(internalValue.AsDynamic._animatedValue); } if (IsExpression(fullValueSource)) { return(internalValue.AsDynamic._expressionValue); } throw new InvalidOperationException("Can't get value of property"); } } DynamicInstance defaultValue = depProperty.AsDynamic._defaultMetadata._defaultValue; return(defaultValue); }
public static TsObject Try(ITsInstance target, TsObject[] args) { var del = (TsDelegate)args[0]; TsObject[] delArgs = null; if (args.Length > 1) { delArgs = new TsObject[args.Length - 1]; Array.Copy(args, 1, delArgs, 0, delArgs.Length); } var result = new DynamicInstance("obj_exception"); try { del.Invoke(delArgs); result["has_error"] = false; result["error_type"] = ""; result["error_msg"] = ""; } catch (Exception e) { result["has_error"] = true; result["error_type"] = e.GetType().Name; result["error_msg"] = e.Message; } return(result); }
public void TestCall1() { //ScriptComplier.Init(); string text = @"using System; using System.Collections; using System.Linq; using System.Text; namespace HelloWorld { public class Test { public Test(){ Name=""111""; } public string Name; public int Age{get;set;} } }"; //根据脚本创建动态类 Type type = ClassBuilder.GetType(text); //创建动态类实例代理 DynamicInstance instance = type; //Get动态调用 Assert.Equal("111", instance["Name"].StringValue); //调用动态委托赋值 instance["Name"].StringValue = "222"; Assert.Equal("222", instance["Name"].StringValue); }
public void CreateMethodInvokerTest() { var m = new Methods.Model(); var di = new DynamicInstance(m); Assert.Equal("11", di.Call("Public", 5, 6)); Assert.Equal("56", di.Call(new Type[] { Types.Int32 }, "GPublic", 5, 6)); var cm = new Methods.ChildModel() { MyProperty = 100 }; var args = new object[] { cm, 0 }; Assert.Equal(100, di.Call("GetMyProperty", args)); Assert.Equal(105, args[1]); Dictionary <string, string> dict = new Dictionary <string, string>(); di = new DynamicInstance(dict); Assert.Equal(false, di.Call("TryGetValue", "a", string.Empty)); dict.Add("a", "axx"); args = new object[] { "a", string.Empty }; Assert.Equal(true, di.Call("TryGetValue", args)); Assert.Equal("axx", args[1]); }
/// <summary> /// Deserializes the data from the reader into a strongly type ConfigurationSection or class /// </summary> /// <param name="xmlNode">The XmlNode containing the serilized data.</param> /// <param name="section">Section that is being deserialized</param> private static void deserializeTypedSection(XmlNode xmlNode, Section section) { var dynSection = DynamicInstance.CreateInstance(section.Assembly, section.Type); if (dynSection != null) { var reader = new XmlNodeReader(xmlNode); if (dynSection is ConfigurationSection) { var deserializeSection = dynSection.GetType().GetMethod("DeserializeSection", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); deserializeSection.Invoke(dynSection, new object[] { reader }); section.Data = dynSection; } else { reader.Read(); reader.MoveToContent(); var xRoot = new XmlRootAttribute(xmlNode.Name); var serializer = new XmlSerializer(dynSection.GetType(), xRoot); section.Data = serializer.Deserialize(reader); } } }
public static TsObject CreateTaskResult(TsObject result, string exception) { var inst = new DynamicInstance("TaffyScript.Threading.TaskResult"); inst["value"] = result; inst["exception"] = exception; return(inst); }
public void DynamicInstanceTest() { var di = new DynamicInstance(new Sum()); Assert.Equal("1", di.Get("Field")); Assert.Equal("1", di.Get("Property")); di = new DynamicInstance(new Sum(), typeof(Father)); Assert.Equal("2", di.Get("Field")); Assert.Equal("1", di.Get("Property")); }
public string DumpObjectBySos(DynamicInstance obj) { if (obj.Type.IsObjectReference) { return(Do(obj.Address)); } else { return(DumpVC(obj.Address, obj.Type.MethodTable)); } }
public void TestCall2() { //创建动态类实例代理 DynamicInstance <TestB> instance = new DynamicInstance <TestB>(); Assert.Equal("111", instance["Name"].StringValue); //调用动态委托赋值 instance["Name"].StringValue = "222"; Assert.Equal("222", instance["Name"].StringValue); }
public void NullableTest() { var m = new NullableClass(); var di = new DynamicInstance(m); di.Set("X", 5); Assert.Equal(m.X, 5); di.Set("X", "6"); Assert.Equal(m.X, 6); di.Set("X", null); Assert.Null(m.X); }
private void CreateDynamicSession() { DynamicInstance = NetworkFilter.IpFilter.Create( Session.Dynamic(), new DisplayData { Name = "ProtonVPN Dynamic Provider" }); DynamicSublayer = DynamicInstance.CreateSublayer(new DisplayData { Name = "ProtonVPN Dynamic Sublayer" }, DynamicSublayerWeight); }
public void CreateFieldSetterTest() { var m = new Fields.Model() { Public = "a" }; var di = new DynamicInstance(m); di.Set("Public", "p-s-v-1"); di.Set("Private", "p-s-v-2"); Assert.Equal("p-s-v-1", m.Public); Assert.Equal("p-s-v-2", di.Get("Private")); di = new DynamicInstance(typeof(Fields.Model)); di.Set("PublicStatic", "p-s-v-3"); Assert.Equal("p-s-v-3", di.Get("PublicStatic")); }
public void TestDynamicInstance() { Person original = new Person("Bruce Lee", 25); dynamic wrapper = new DynamicInstance(original); Assert.Equal(original.Name, wrapper.Name); Assert.Equal(original.Age, wrapper.Age); double distance; original.Walk(10d, out distance); Assert.Equal(10d, distance); wrapper.Walk(10d, out distance); //Assert.Equal( 20d, distance ); Assert.Equal(20d, original.TryGetFieldValue("metersTravelled")); wrapper.Age = 26; Assert.Equal(26, wrapper.Age); Assert.Throws <Microsoft.CSharp.RuntimeBinder.RuntimeBinderException>(() => wrapper.NotFoundMethod()); Assert.Equal("get_A", new DynamicInstance(new { A = "a" }).GetDynamicMemberNames().First()); }
public void CreateFieldGetterTest() { var di = new DynamicInstance(new Fields.Model() { Public = "a" }); Assert.Equal("a", di.Get("Public")); Assert.Null(di.Get("Private")); di = new DynamicInstance(new Fields.Model <int>() { Public = 5 }); Assert.Equal(5, di.Get("Public")); di = new DynamicInstance(typeof(Fields.Model)); Fields.Model.PublicStatic = "p-s-v"; Assert.Equal("p-s-v", di.Get("PublicStatic")); }
public void CreatePropertySetterTest() { var m = new Properties.Model() { Public = "a" }; var di = new DynamicInstance(m); di.Set("Public", "p-s-v-1"); di.Set("Private", "p-s-v-2"); Assert.Equal("p-s-v-1", m.Public); Assert.Equal("p-s-v-2", di.Get("Private")); di.Set("PublicArray", new string[] { "1", "2", "3" }); Assert.Equal(new string[] { "1", "2", "3" }, m.PublicArray); di = new DynamicInstance(typeof(Properties.Model)); di.Set("PublicStatic", "p-s-v-3"); Assert.Equal("p-s-v-3", di.Get("PublicStatic")); }
public static TsObject TimeInvoke(ITsInstance target, TsObject[] args) { var del = (TsDelegate)args[0]; TsObject[] delArgs = null; if (args.Length > 1) { delArgs = new TsObject[args.Length - 1]; Array.Copy(args, 1, delArgs, 0, delArgs.Length); } var timer = new Stopwatch(); timer.Start(); del.Invoke(delArgs); timer.Stop(); var result = new DynamicInstance("obj_timer_result"); result["ms"] = timer.ElapsedMilliseconds; result["ticks"] = timer.ElapsedTicks; return(result); }
public void StructTest() { var box = DynamicFactory.CreateInstance(typeof(MyStruct)); DynamicInstance di = new DynamicInstance(box); di.Set("Field", 6); di.Set("Property", "AAA"); di.Set("ChildStruct", new ChildStruct() { Field = 12 }); var str = (MyStruct)box; Assert.Equal(6, str.Field); Assert.Equal("AAA", str.Property); Assert.Equal(12, str.ChildStruct.Field); di = new DynamicInstance(str); Assert.Equal(6, di.Get("Field")); Assert.Equal("AAA", di.Get("Property")); Assert.Equal(12, ((ChildStruct)di.Get("ChildStruct")).Field); }
static void Main(string[] args) { /* * 在此之前,你需要右键,选择工程文件,在你的.csproj里面 * * 写上这样一句浪漫的话: * * <PreserveCompilationContext>true</PreserveCompilationContext> */ var delegateAction = MethodBuilder.NewMethod .Using(typeof(Console)) .Param <string>("str1") .Param <string>("str2") .Body(@" string result = str1 +"" ""+ str2; Console.WriteLine(result); ") .Return() .Create(); ((Action <string, string>)delegateAction)("Hello", "World!"); var delegateAction2 = MethodBuilder.NewMethod .Using(typeof(Console)) .Param <string>("str1") .Param <string>("str2") .Body(@" string result = str1 +"" ""+ str2; Console.WriteLine(result); ") .Return() .Create <Action <string, string> >(); delegateAction2("Hello", "World!"); string text = @"using System; using System.Collections; using System.Linq; using System.Text; namespace HelloWorld { public class Test { public Test(){ Name=""111""; } public string Name; public int Age{get;set;} } }"; //根据脚本创建动态类 Type type = ClassBuilder.GetType(text); //创建动态类实例代理 DynamicInstance instance = type; if (instance["Name"].StringValue == "111") { //调用动态委托赋值 instance["Name"].StringValue = "222"; } //调用动态类 Console.WriteLine(instance["Name"].StringValue); //创建动态类实例代理 DynamicInstance <TestB> instance2 = new DynamicInstance <TestB>(); if (instance2["Name"].StringValue == "111") { //调用动态委托赋值 instance2["Name"].StringValue = "222"; } //调用动态类 Console.WriteLine(instance2["Name"].StringValue); Console.ReadKey(); }
public static void DynamicMap(object source, object destination, Type sourceType, Type destinationType) { DynamicInstance.DynamicMap(source, destination, sourceType, destinationType); }
public static object DynamicMap(object source, Type sourceType, Type destinationType) { return(DynamicInstance.DynamicMap(source, sourceType, destinationType)); }
public static TDestination DynamicMap <TSource, TDestination>(TSource source) { return(DynamicInstance.DynamicMap <TSource, TDestination>(source)); }
public DepPropertyInfo(ClrType propertyType, DynamicInstance propertyValue, string propertyName) { Type = propertyType; Value = propertyValue; Name = propertyName; }
public static void DynamicMap <TSource, TDestination>(TSource source, TDestination destination) { DynamicInstance.DynamicMap(source, destination); }
public static TDestination DynamicMap <TDestination>(object source) { return(DynamicInstance.DynamicMap <TDestination>(source)); }