Challenge: Reflection
メニューを表示するにはスワイプしてください
In summary, Your task is to fill in the blanks where necessary to implement reflection.
The task is explained in the following video:
Hint
- The
GetProperties()method of theTypecode> class returns an array of all the public properties in a Type. - The
GetProperty(propertyName)code> method of theTypecode> class returns a reference to a single property which matches thepropertyName. - The
SetValue(object, value)method of thePropertyInfoclass sets the property value of theobjecttovalue.
Solution
using System.Reflection;
using System.Security.AccessControl;
namespace ObjectManagerTask
{
class Product
{
private static int Products = 0;
private int ID { get; }
public string Name { get; set; }
public float Price { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public Product()
{
this.Name = "";
this.Price = 0.0f;
this.ID = Products++;
this.Description = "";
}
}
public partial class MainPage : ContentPage
{
List<Product> products = new List<Product>();
public MainPage()
{
InitializeComponent();
// Store the type of product class
Type productType = typeof(Product);
// Get all the properties of the Product class
PropertyInfo[] properties = productType.GetProperties();
// Loop through all the properties
foreach (PropertyInfo property in properties)
{
Entry entryBox = new Entry();
entryBox.Placeholder = property.Name;
fields.Children.Add(entryBox);
}
createBtn.Clicked += CreateProduct;
}
void CreateProduct(object? sender, EventArgs e)
{
try
{
Product product = new Product();
// Store the type of product class
Type productType = product.GetType();
foreach (Entry entryBox in fields.Children)
{
// Get the property reference from the Product class, which has the same name as 'entryBox.Placeholder'.
// The exclamation mark ensures the compiler that the retrieved reference is not going to be null.
// It is to suppress the warning.
PropertyInfo property = productType.GetProperty(entryBox.Placeholder)!;
object value = Convert.ChangeType(entryBox.Text, property.PropertyType);
property.SetValue(product, value);
}
products.Add(product);
}
catch
{
infoLabel.Text = $"ERROR: Invalid value in one of the fields.";
return;
}
infoLabel.Text = $"Product {products.Count} created successfully.";
ClearEntryBoxes();
UpdateProductsList();
}
void UpdateProductsList()
{
productsList.Children.Clear();
// Store the type of Product class
Type productType = typeof(Product);
foreach (Product product in products)
{
Label dataLabel = new Label();
// Retrieve all the properties of productType
PropertyInfo[] properties = productType.GetProperties();
// Loop through all the properties
foreach (PropertyInfo property in properties)
{
// Get the value of 'property' from the 'product' object.
// The exclamation mark is to suppress the compiler warning since we know that the property exists,
// and it cannot return null.
object value = property.GetValue(product)!;
dataLabel.Text += $"{property.Name}: {value.ToString()}\t";
}
productsList.Children.Add(dataLabel);
}
}
void ClearEntryBoxes()
{
foreach (Entry entryBox in fields.Children)
{
entryBox.Text = "";
}
}
}
}
Video
すべて明確でしたか?
フィードバックありがとうございます!
セクション 4. 章 8
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 4. 章 8