You could implement this by yourself like so:
public delegate void AllPropertiesSetDelegate();
public class Test
{
public delegate void AllPropertiesSetDelegate(object sender, EventArgs args);
public int Id
{
get => _id;
set
{
_id = value;
CheckAllProperties();
}
}
private int _id;
public string Name
{
get => _name;
set
{
_name = value;
CheckAllProperties();
}
}
private string _name;
private void CheckAllProperties()
{
//Comparing Id to null is pointless here because it is not nullable.
if (Name != null && Id != null)
{
AllPropertiesSet?.Invoke(this, new EventArgs());
}
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.AllPropertiesSet += delegate { AllPropsSet(); };
t.Id = 1;
t.Name = "asd";
Console.ReadKey();
}
static void AllPropsSet()
{
Console.WriteLine("All properties have been set.");
}
}
See for yourself if you can get the implementation smaller/less painfull to deal with.
Test code:
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.AllPropertiesSet += delegate { AllPropsSet(); };
t.Id = 1;
t.Name = "asd";
Console.ReadKey();
}
static void AllPropsSet()
{
Console.WriteLine("All properties have been set.");
}
}
Heres how you could use reflection to check all non-value types for null:
public static bool AllPropertiesNotNull<T>(this T obj) where T : class
{
foreach (var prop in obj.GetType().GetProperties())
{
//See if our property is not a value type (value types can't be null)
if (!prop.PropertyType.IsValueType)
{
if (prop.GetValue(obj, null) == null)
{
return false;
}
}
}
return true;
}
You can consume this in the original code by modifying the CheckAllProperties method:
private void CheckAllProperties()
{
if (this.AllPropertiesNotNull())
{
AllPropertiesSet?.Invoke(this, new EventArgs());
}
}