Home   Cover Cover Cover Cover
 

Query-Ausdruck auf Metadaten

A07.cs
/* Query expression applied to metadata
   This example uses a query expression to produce a list of all
   properties of a give type.
------------------------------------------------------------*/
using System;
using System.Linq;

public class A7 {
  
  public static void Main() {
    Type t = typeof(System.Collections.ArrayList);
    
    var result =
      from p in t.GetProperties()
      where p.DeclaringType == t
      select new { p.Name, Type = p.PropertyType.Name, p.CanRead, p.CanWrite };
    
    Console.WriteLine("Properties of type " + t.Name);
    foreach (var property in result) {
      Console.WriteLine(
        "  " +
        property.Type + " " + 
        property.Name + " { " + 
        (property.CanRead ? "get;" : "") + 
        (property.CanWrite ? " set;" : "") + " };"
      );
    }
  }
}