Logo
Section 1 / 7

动态语言运行时

学习如何绕过编译时类型检查,并使用动态语言运行时在执行时评估对象成员。

导入

动态语言运行时

学习如何绕过编译时类型检查,并使用动态语言运行时在执行时评估对象成员。

1namespace DynamicLanguageRuntime; 2 3public class Course 4{ 5 public string Title { get; set; } = string.Empty; 6}
1using DynamicLanguageRuntime; 2 3var course = new Course() { Title = ".NET Fundamentals" }; 4 5// Duration property does not exist 6// We will get an error during compilation 7Console.WriteLine(course.Duration);

为动态演示复用的原始 Course 类

1namespace DynamicLanguageRuntime; 2 3public class Course 4{ 5 public string Title { get; set; } = string.Empty; 6}
1using DynamicLanguageRuntime; 2 3// We declare the variable using the dynamic keyword 4dynamic course = new Course() { Title = ".NET Fundamentals" }; 5 6// Program compiles and runs because there is no compile-time checking 7// for dynamic objects 8Console.WriteLine("Program started running..."); 9 10// However, because there is no Duration property, 11// the program fails during runtime 12Console.WriteLine(course.Duration);
1// We assign an int value in the beginning 2dynamic someVariable = 24; 3 4Console.WriteLine($"Type of someVariable: {someVariable.GetType()}, value: {someVariable}."); 5 6// Can assign string 7someVariable = "Now, it's a string"; 8 9Console.WriteLine($"Type of someVariable: {someVariable.GetType()}, value: {someVariable}.");
1namespace DynamicLanguageRuntime; 2 3public class Course 4{ 5 public string Title { get; set; } = string.Empty; 6 public dynamic Duration { get; set; } 7}
1using DynamicLanguageRuntime; 2 3var course = new Course() { Title = ".NET Fundamentals" }; 4 5// We set the value of the Duration property to an int object first 6course.Duration = 8; // 8 weeks 7Console.WriteLine($"Duration property type: {course.Duration.GetType()}, value: {course.Duration}."); 8 9// Now we set the value to a string object 10course.Duration = "8 weeks"; 11Console.WriteLine($"Duration property type: {course.Duration.GetType()}, value: {course.Duration}.");
1namespace DynamicLanguageRuntime; 2 3public class Course 4{ 5 public string Title { get; set; } = string.Empty; 6 7 public int DurationInWeeks { get; set; } 8 9 // The method has dynamic return type 10 public dynamic GetCourseDuration(string format) 11 { 12 // Depending on the value of format, 13 // the return type will either be int or string 14 if (format == "string") 15 { 16 return $"{DurationInWeeks} weeks"; 17 } 18 else if (format == "int") 19 { 20 return DurationInWeeks; 21 } 22 else 23 { 24 return "Invalid format provided."; 25 } 26 } 27}
1using DynamicLanguageRuntime; 2 3var course = new Course() { Title = ".NET Fundamentals", DurationInWeeks = 8 }; 4 5// GetCourseDuration has the dynamic return type 6dynamic duration = course.GetCourseDuration("string"); 7Console.WriteLine($"Type: {duration.GetType()}, value: {duration}"); 8 9duration = course.GetCourseDuration("int"); 10Console.WriteLine($"Type: {duration.GetType()}, value: {duration}");
1using System.Text.Json.Nodes; 2 3string jsonString = """{ "Name": "Alice", "Role": "Engineer" }"""; 4 5// Parse the JSON string into a dynamic node 6dynamic user = JsonNode.Parse(jsonString); 7 8// Use indexers to access dynamic JSON properties 9Console.WriteLine($"User: {user["Name"]}, Role: {user["Role"]}");

反射

ExpandoObject 类