Pages

Showing posts with label David Bush. Show all posts
Showing posts with label David Bush. Show all posts

Monday, November 3, 2008

REFLECTION AND ATTRIBUTES (Part One)

Today I decided to post a series of tutorials about Reflection and Attributes from the free chapter of Core C# book.  So I'm going to start it with Reflection (Creating a Type Object).  Read the first part of the chapter below:

 

19.1 Reflection

  In general terms, reflection is the ability of a system to reason and act upon itself.  More specifically, .NET supports reflection as a mechanism for reading an assembly’s metadata in order to glean information about types within the assembly. It has many uses. For example, VS.NET uses it to implement Intellisense; and .NET object serialization relies on reflection to identify whether a type can be serialized (by looking for the SerializableAttribute) and which of its members are serializable.

While reflection is a powerful technique, it’s not difficult to understand. Think of the reflection API as a set of methods that can be applied at each level of the .NET coding hierarchy. The Assembly.GetTypes method yields all the types in an assembly
as System.Type objects. The System.Type class, in turn, exposes methods that return a specific type’s properties, methods, constructors, events, and fields. At an even more granular level, you can then examine the parameters of a method or constructor.

Before a type (class, struct, array, enum, generic types, value types, or interface) can be reflected, a Type reference must be acquired for the type. This reference is the key that unlocks the door to the .NET reflection API.

 

The System.Type class

System.Type is an abstract class whose members provide the primary means of accessing the metadata that describes a type.

Syntax:
public abstract class Type : MemberInfo, _Type, IReflect

The Type class inherits from the MemberInfo class and two interfaces—_Type and IReflect. Of these, MemberInfo is of particular interest. It serves as the base class for some of the most important classes in the reflection namespace, and defines
many of the methods and properties that provide information about a type’s members and attributes. We’ll look at it in detail shortly.


Creating a Type Object

In order to use reflection to examine a type’s metadata, it is first necessary to obtain a reference to a Type object that is associated with the type to be reflected. There are several ways to obtain a Type object reference. The choice varies depending on whether the object is being created from a type’s definition or an instance of the type.  To illustrate the first case, consider the following code segment that lists the methods for the System.Object class.

// (1) Get Type for System.Object definition
          
Type obType = typeof(System.Object);
            // Alternative: Type obType= Type.GetType("System.Object");
            // (2) Call a method to return list of methods for Object
          
MethodInfo[] mi = obType.GetMethods();
            for(int i = 0; i < mi.Length; i++)
            {
                MethodInfo myMethodInfo = (MethodInfo)mi[i];
                Console.WriteLine("Method name: {0}({1})",
                myMethodInfo.Name, myMethodInfo.ReturnType);
                /* Output:
                Method Name: GetHashCode (System.Int32)
                Method Name: Equals (System.Boolean)
                (remaining methods listed here)
                */
          
}


This example uses the C# typeof operator to obtain a Type reference. Alternatively, the Type.GetType method, which accepts a type’s name as a string value, could be used. Once the Type object is obtained, the Type.GetMethods method is called to return an array of MethodInfo objects that represent the methods for the type. The Name and ReturnType properties of the MethodInfo class expose the name and return type of each method.  To acquire a Type object reference for an instance of a type—rather than its definition—use the GetType method that all types inherit from System.Object:

Device myDevice = newDevice(); // Create class instance
Type t = myDevice.GetType(); // GetType reference
// Reflection can also be used on value types
int[] ages = { 22, 43, 55 };

Type ageType = ages.GetType();
Console.WriteLine(ageType.Name); // Output: Int32[]

Later in this section, we’ll see how to use the Assembly.GetTypes and Module.GetTypes methods to obtain Type object references for types in an assemblyand module, respectively.

So there it goes that's the first part of chapter 19 of the book Core C#, on the next series I am going to post the System.Type Members

Related topic:

REFLECTION AND ATTRIBUTES (Part Two)

Related Post:

Sunday, July 20, 2008

What is the code inside GetProductList() List on 101 LINQ Samples?

Somebody asked me what the code is inside GetProductList() on MSDN 101 LINQ Samples. So what I did was to do some research on MSDN and I found this link.

Then I started to modify the code and voila! I got it working on my previous post about 101 LINQ Samples.

Here is the modified code I've done.


public static List<Product> GetProductList()
{
var productList = new List<Product>();
productList.Add( new Product(){ ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 });
productList.Add( new Product(){ ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 });
productList.Add( new Product(){ ProductID = 3, ProductName = "Aniseed Syrup", Category = "Condiments", UnitPrice = 10.0000M, UnitsInStock = 13 });
productList.Add( new Product(){ ProductID = 4, ProductName = "Chef Anton's Cajun Seasoning", Category = "Condiments", UnitPrice = 22.0000M, UnitsInStock = 53 });
productList.Add( new Product(){ ProductID = 5, ProductName = "Chef Anton's Gumbo Mix", Category = "Condiments", UnitPrice = 21.3500M, UnitsInStock = 0 });
productList.Add( new Product(){ ProductID = 6, ProductName = "Grandma's Boysenberry Spread", Category = "Condiments", UnitPrice = 25.0000M, UnitsInStock = 120 });
productList.Add( new Product(){ ProductID = 7, ProductName = "Uncle Bob's Organic Dried Pears", Category = "Produce", UnitPrice = 30.0000M, UnitsInStock = 15 });
productList.Add( new Product(){ ProductID = 8, ProductName = "Northwoods Cranberry Sauce", Category = "Condiments", UnitPrice = 40.0000M, UnitsInStock = 6 });
productList.Add( new Product(){ ProductID = 9, ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = 29 });
productList.Add( new Product(){ ProductID = 10, ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = 31 });
productList.Add( new Product(){ ProductID = 11, ProductName = "Queso Cabrales", Category = "Dairy Products", UnitPrice = 21.0000M, UnitsInStock = 22 });
productList.Add( new Product(){ ProductID = 12, ProductName = "Queso Manchego La Pastora", Category = "Dairy Products", UnitPrice = 38.0000M, UnitsInStock = 86 });
productList.Add( new Product(){ ProductID = 13, ProductName = "Konbu", Category = "Seafood", UnitPrice = 6.0000M, UnitsInStock = 24 });
productList.Add( new Product(){ ProductID = 14, ProductName = "Tofu", Category = "Produce", UnitPrice = 23.2500M, UnitsInStock = 35 });
productList.Add( new Product(){ ProductID = 15, ProductName = "Genen Shouyu", Category = "Condiments", UnitPrice = 15.5000M, UnitsInStock = 39 });
productList.Add( new Product(){ ProductID = 16, ProductName = "Pavlova", Category = "Confections", UnitPrice = 17.4500M, UnitsInStock = 29 });
productList.Add( new Product(){ ProductID = 17, ProductName = "Alice Mutton", Category = "Meat/Poultry", UnitPrice = 39.0000M, UnitsInStock = 0 });
productList.Add( new Product(){ ProductID = 18, ProductName = "Carnarvon Tigers", Category = "Seafood", UnitPrice = 62.5000M, UnitsInStock = 42 });
productList.Add( new Product(){ ProductID = 19, ProductName = "Teatime Chocolate Biscuits", Category = "Confections", UnitPrice = 9.2000M, UnitsInStock = 25 });
productList.Add( new Product(){ ProductID = 20, ProductName = "Sir Rodney's Marmalade", Category = "Confections", UnitPrice = 81.0000M, UnitsInStock = 40 });
productList.Add( new Product(){ ProductID = 21, ProductName = "Sir Rodney's Scones", Category = "Confections", UnitPrice = 10.0000M, UnitsInStock = 3 });
productList.Add( new Product(){ ProductID = 22, ProductName = "Gustaf's Knäckebröd", Category = "Grains/Cereals", UnitPrice = 21.0000M, UnitsInStock = 104 });
productList.Add( new Product(){ ProductID = 23, ProductName = "Tunnbröd", Category = "Grains/Cereals", UnitPrice = 9.0000M, UnitsInStock = 61 });
productList.Add( new Product(){ ProductID = 24, ProductName = "Guaraná Fantástica", Category = "Beverages", UnitPrice = 4.5000M, UnitsInStock = 20 });
productList.Add( new Product(){ ProductID = 25, ProductName = "NuNuCa Nuß-Nougat-Creme", Category = "Confections", UnitPrice = 14.0000M, UnitsInStock = 76 });
productList.Add( new Product(){ ProductID = 26, ProductName = "Gumbär Gummibärchen", Category = "Confections", UnitPrice = 31.2300M, UnitsInStock = 15 });
productList.Add( new Product(){ ProductID = 27, ProductName = "Schoggi Schokolade", Category = "Confections", UnitPrice = 43.9000M, UnitsInStock = 49 });
productList.Add( new Product(){ ProductID = 28, ProductName = "Rössle Sauerkraut", Category = "Produce", UnitPrice = 45.6000M, UnitsInStock = 26 });
productList.Add( new Product(){ ProductID = 29, ProductName = "Thüringer Rostbratwurst", Category = "Meat/Poultry", UnitPrice = 123.7900M, UnitsInStock = 0 });
productList.Add( new Product(){ ProductID = 30, ProductName = "Nord-Ost Matjeshering", Category = "Seafood", UnitPrice = 25.8900M, UnitsInStock = 10 });
productList.Add( new Product(){ ProductID = 31, ProductName = "Gorgonzola Telino", Category = "Dairy Products", UnitPrice = 12.5000M, UnitsInStock = 0 });
productList.Add( new Product(){ ProductID = 32, ProductName = "Mascarpone Fabioli", Category = "Dairy Products", UnitPrice = 32.0000M, UnitsInStock = 9 });
productList.Add( new Product(){ ProductID = 33, ProductName = "Geitost", Category = "Dairy Products", UnitPrice = 2.5000M, UnitsInStock = 112 });
productList.Add( new Product(){ ProductID = 34, ProductName = "Sasquatch Ale", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 111 });
productList.Add( new Product(){ ProductID = 35, ProductName = "Steeleye Stout", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 20 });
productList.Add( new Product(){ ProductID = 36, ProductName = "Inlagd Sill", Category = "Seafood", UnitPrice = 19.0000M, UnitsInStock = 112 });
productList.Add( new Product(){ ProductID = 37, ProductName = "Gravad lax", Category = "Seafood", UnitPrice = 26.0000M, UnitsInStock = 11 });
productList.Add( new Product(){ ProductID = 38, ProductName = "Côte de Blaye", Category = "Beverages", UnitPrice = 263.5000M, UnitsInStock = 17 });
productList.Add( new Product(){ ProductID = 39, ProductName = "Chartreuse verte", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 69 });
productList.Add( new Product(){ ProductID = 40, ProductName = "Boston Crab Meat", Category = "Seafood", UnitPrice = 18.4000M, UnitsInStock = 123 });
productList.Add( new Product(){ ProductID = 41, ProductName = "Jack's New England Clam Chowder", Category = "Seafood", UnitPrice = 9.6500M, UnitsInStock = 85 });
productList.Add( new Product(){ ProductID = 42, ProductName = "Singaporean Hokkien Fried Mee", Category = "Grains/Cereals", UnitPrice = 14.0000M, UnitsInStock = 26 });
productList.Add( new Product(){ ProductID = 43, ProductName = "Ipoh Coffee", Category = "Beverages", UnitPrice = 46.0000M, UnitsInStock = 17 });
productList.Add( new Product(){ ProductID = 44, ProductName = "Gula Malacca", Category = "Condiments", UnitPrice = 19.4500M, UnitsInStock = 27 });
productList.Add( new Product(){ ProductID = 45, ProductName = "Rogede sild", Category = "Seafood", UnitPrice = 9.5000M, UnitsInStock = 5 });
productList.Add( new Product(){ ProductID = 46, ProductName = "Spegesild", Category = "Seafood", UnitPrice = 12.0000M, UnitsInStock = 95 });
productList.Add( new Product(){ ProductID = 47, ProductName = "Zaanse koeken", Category = "Confections", UnitPrice = 9.5000M, UnitsInStock = 36 });
productList.Add( new Product(){ ProductID = 48, ProductName = "Chocolade", Category = "Confections", UnitPrice = 12.7500M, UnitsInStock = 15 });
productList.Add( new Product(){ ProductID = 49, ProductName = "Maxilaku", Category = "Confections", UnitPrice = 20.0000M, UnitsInStock = 10 });
productList.Add( new Product(){ ProductID = 50, ProductName = "Valkoinen suklaa", Category = "Confections", UnitPrice = 16.2500M, UnitsInStock = 65 });
productList.Add( new Product(){ ProductID = 51, ProductName = "Manjimup Dried Apples", Category = "Produce", UnitPrice = 53.0000M, UnitsInStock = 20 });
productList.Add( new Product(){ ProductID = 52, ProductName = "Filo Mix", Category = "Grains/Cereals", UnitPrice = 7.0000M, UnitsInStock = 38 });
productList.Add( new Product(){ ProductID = 53, ProductName = "Perth Pasties", Category = "Meat/Poultry", UnitPrice = 32.8000M, UnitsInStock = 0 });
productList.Add( new Product(){ ProductID = 54, ProductName = "Tourtière", Category = "Meat/Poultry", UnitPrice = 7.4500M, UnitsInStock = 21 });
productList.Add( new Product(){ ProductID = 55, ProductName = "Pâté chinois", Category = "Meat/Poultry", UnitPrice = 24.0000M, UnitsInStock = 115 });
productList.Add( new Product(){ ProductID = 56, ProductName = "Gnocchi di nonna Alice", Category = "Grains/Cereals", UnitPrice = 38.0000M, UnitsInStock = 21 });
productList.Add( new Product(){ ProductID = 57, ProductName = "Ravioli Angelo", Category = "Grains/Cereals", UnitPrice = 19.5000M, UnitsInStock = 36 });
productList.Add( new Product(){ ProductID = 58, ProductName = "Escargots de Bourgogne", Category = "Seafood", UnitPrice = 13.2500M, UnitsInStock = 62 });
productList.Add( new Product(){ ProductID = 59, ProductName = "Raclette Courdavault", Category = "Dairy Products", UnitPrice = 55.0000M, UnitsInStock = 79 });
productList.Add( new Product(){ ProductID = 60, ProductName = "Camembert Pierrot", Category = "Dairy Products", UnitPrice = 34.0000M, UnitsInStock = 19 });
productList.Add( new Product(){ ProductID = 61, ProductName = "Sirop d'érable", Category = "Condiments", UnitPrice = 28.5000M, UnitsInStock = 113 });
productList.Add( new Product(){ ProductID = 62, ProductName = "Tarte au sucre", Category = "Confections", UnitPrice = 49.3000M, UnitsInStock = 17 });
productList.Add( new Product(){ ProductID = 63, ProductName = "Vegie-spread", Category = "Condiments", UnitPrice = 43.9000M, UnitsInStock = 24 });
productList.Add( new Product(){ ProductID = 64, ProductName = "Wimmers gute Semmelknödel", Category = "Grains/Cereals", UnitPrice = 33.2500M, UnitsInStock = 22 });
productList.Add( new Product(){ ProductID = 65, ProductName = "Louisiana Fiery Hot Pepper Sauce", Category = "Condiments", UnitPrice = 21.0500M, UnitsInStock = 76 });
productList.Add( new Product(){ ProductID = 66, ProductName = "Louisiana Hot Spiced Okra", Category = "Condiments", UnitPrice = 17.0000M, UnitsInStock = 4 });
productList.Add( new Product(){ ProductID = 67, ProductName = "Laughing Lumberjack Lager", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 52 });
productList.Add( new Product(){ ProductID = 68, ProductName = "Scottish Longbreads", Category = "Confections", UnitPrice = 12.5000M, UnitsInStock = 6 });
productList.Add( new Product(){ ProductID = 69, ProductName = "Gudbrandsdalsost", Category = "Dairy Products", UnitPrice = 36.0000M, UnitsInStock = 26 });
productList.Add( new Product(){ ProductID = 70, ProductName = "Outback Lager", Category = "Beverages", UnitPrice = 15.0000M, UnitsInStock = 15 });
productList.Add( new Product(){ ProductID = 71, ProductName = "Flotemysost", Category = "Dairy Products", UnitPrice = 21.5000M, UnitsInStock = 26 });
productList.Add( new Product(){ ProductID = 72, ProductName = "Mozzarella di Giovanni", Category = "Dairy Products", UnitPrice = 34.8000M, UnitsInStock = 14 });
productList.Add( new Product(){ ProductID = 73, ProductName = "Röd Kaviar", Category = "Seafood", UnitPrice = 15.0000M, UnitsInStock = 101 });
productList.Add( new Product(){ ProductID = 74, ProductName = "Longlife Tofu", Category = "Produce", UnitPrice = 10.0000M, UnitsInStock = 4 });
productList.Add( new Product(){ ProductID = 75, ProductName = "Rhönbräu Klosterbier", Category = "Beverages", UnitPrice = 7.7500M, UnitsInStock = 125 });
productList.Add( new Product(){ ProductID = 76, ProductName = "Lakkalikööri", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 57 });
productList.Add( new Product(){ ProductID = 77, ProductName = "Original Frankfurter grüne Soße", Category = "Condiments", UnitPrice = 13.0000M, UnitsInStock = 32 });
return productList;
}
}










I just put the code inside the Generic List and then change a little on adding the data. I created a Product class and use it as parameter on my Generic List. Here is my code on Product class.







public class Product
{
public int ProductID { get; set; }

public string ProductName { get; set; }

public string Category { get; set; }

public decimal UnitPrice { get; set; }

public int UnitsInStock { get; set; }
}





After that I added some code inside the Linq2 method. The original code is something like this;


public void Linq2() {
List products = GetProductList();

var soldOutProducts = from p in products
where p.UnitsInStock == 0
select p;
Console.WriteLine("Sold out products:");
foreach (var product in soldOutProducts) {
Console.WriteLine("{0} is sold out!", product.ProductName);
}
}

I had changed the declaration of products variable.

public static void Linq2()
{
IEnumerable<Product> products = GetProductList();

var soldOutProducts =
from p in products
where p.UnitsInStock == 0
select p;

Console.WriteLine("Sold out products:");
foreach (var product in soldOutProducts)
{
Console.WriteLine("{0} is sold out!", product.ProductName);
}
}





I am having fun experimenting with LINQ. I will just continue posting what I am getting.



Reference url:


Rediscovering IEnumerable<T>


Restriction Operators


GetProductList code




Reference video:

















kick it on DotNetKicks.com

Sunday, June 29, 2008

Using LINQ

I tried to look for some examples on youtube on how to use LINQ and I found this video tutorial on how to use LINQ. It contain a code for C# and for VB. I believe this cool video is a good start for those who want to learn LINQ... Thanks to David Bush.














kick it on DotNetKicks.com