public object TryCreateUniqueTemplate(Type type) { var constructor = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic) .OrderByDescending(x => x.GetParameters().Count()) .FirstOrDefault(); if (constructor == null) { throw new InvalidOperationException("Cannot find a suitable constructor for query planning for type " + type.FullNameInCode()); } var valueSource = new UniqueValueSource(); var ctorArgs = valueSource.ArgsFor(constructor); var query = Activator.CreateInstance(type, ctorArgs); if (AreAllMemberValuesUnique(query)) { return(query); } foreach (var queryMember in Parameters) { queryMember.TryWriteValue(valueSource, query); } if (AreAllMemberValuesUnique(query)) { return(query); } throw new InvalidCompiledQueryException("Marten is unable to create a compiled query plan for type " + type.FullNameInCode()); }
public void TryWriteValue(UniqueValueSource valueSource, object query) { if (CanWrite()) { SetValue(query, (T)valueSource.GetValue(typeof(T))); } }
public void TryWriteValue(UniqueValueSource valueSource, object query) { if (CanWrite()) { var value = (T)valueSource.GetValue(typeof(T)); Value = value; SetValue(query, value); } }