CSharp · Console
using System;
using System.Reflection;
public class Program
{
public static void Main(string[] args)
{
Logic logic = new Logic();
try
{
logic.MyProperty = 5;
}
catch(Exception e)
{ // in this case, the exception is caught here.
Console.WriteLine("Exception was caught: " + e.Message);
}
try{
PropertyInfo propInfo = logic.GetType().GetProperty("MyProperty");
propInfo.SetValue(logic,5, null);
}
catch (Exception e)
{
Console.WriteLine("Reflection exception was caught: " + e.InnerException.Message);
}
}
public class Logic
{
private double variable;
public double MyProperty
{
get
{
return variable;
}
set
{ //Check if value is valid and if not throw an exception
throw new Exception("This is the exception");
}
}
}
}