C# – Define the Allow / Reject Class Type on the Generic Function
public void SetRecord<T>(T obj) where T : class { if (this.IsAllowSetRecordType(obj)) { /* … … */ return obj; } else { throw new Exception("Type – " + obj.GetType() + " is not allowed on the < SetRecord > Function."); } } public bool IsAllowSetRecordType<T>(T obj) where T : class { List<Type> allowType = this.getSetRecordAllowType(); if (allowType.Contains(obj.GetType())) return true; else return false; } public List<Type> getSetRecordAllowType() { List<Type> allowType = new List<Type>(); /* Most Likely, it is the Entity Framework – Model Class */ allowType.Add(typeof(… …)); allowType.Add(typeof(… …)); allowType.Add(typeof(… …)); return allowType; } |