using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ModelAndCallback
{
class Program
{
static void Main(string[] args)
{
ProductFactory productFactory = new ProductFactory();
WrapFactory wrapFactory = new WrapFactory();
Func<Product> func1 = new Func<Product>(productFactory.MakePizze);
Func<Product> func2 = new Func<Product>(productFactory.MakeToyCar);
//Box box1 = wrapFactory.WrapProduct(func1);
//Box box2 = wrapFactory.WrapProduct(func2);
//Console.WriteLine(box1.Product.Name);
//Console.WriteLine(box2.Product.Name);
Console.WriteLine(func1.Invoke().Name);
Console.WriteLine(func2.Invoke().Name);
}
}
class Product
{
public string Name { get; set; }
}
class Box
{
public Product Product { get; set; }
}
class WrapFactory
{
public Box WrapProduct(Func<Product> getProduct)
{
Box box = new Box();
box.Product = getProduct.Invoke();
return box;
}
}
class ProductFactory
{
public Product MakePizze()
{
Product product = new Product();
product.Name = "Pizze";
return product;
}
public Product MakeToyCar()
{
Product product = new Product();
product.Name = "ToyCar";
return product;
}
}
}
图一中注释掉的写法与没有注释掉的写法debug出来的结果都是一样的,因为注释的语句看不懂,所以想问下注释部分的解释以及我没注释部分的写法是否合理。求大神解答下,谢谢!
都是取product.name,没毛病啊