private static bool TryBindImplementation( Type targetType, Type defaultType = null) { targetType = MemenimScriptUtils.GetBaseType(targetType); if (defaultType == targetType) { defaultType = null; } if (!typeof(IMemenimScriptBindable).IsAssignableFrom(targetType)) { return(false); } if (defaultType != null && !targetType.IsAssignableFrom(defaultType)) { return(false); } var result = TryGetImplementation( targetType, defaultType); if (!result.IsSuccess) { return(false); } var reference = MemenimScriptUtils.GetBindReference(targetType); reference.Value = result.Implementation; return(true); }
public static void BindImplementation <T>( MemenimScriptBindTargetType target, T value) where T : class { if (target == MemenimScriptBindTargetType.Unknown) { return; } if (value == null) { return; } if (!(value is IMemenimScriptBindable)) { throw new ArgumentException( $"{nameof(value)} must implement the {nameof(IMemenimScriptBindable)} interface", nameof(value)); } Type baseType = MemenimScriptUtils.GetBaseType(target); if (baseType == null) { return; } if (!baseType.IsAssignableFrom(value.GetType())) { throw new ArgumentException( $"{nameof(value)} must be derived from the target[{target}] base class (in this case - {baseType.Name})", nameof(value)); } var reference = MemenimScriptUtils.GetBindReference(target); reference.Value = (IMemenimScriptBindable)value; }
private static void BindImplementationInternal <T>( T value, T defaultValue = null) where T : class, IMemenimScriptBindable { var targetType = value?.GetType() ?? typeof(T); var defaultType = defaultValue?.GetType(); targetType = MemenimScriptUtils.GetBaseType(targetType); if (defaultType == targetType) { defaultType = null; } if (TryBindImplementation(targetType, defaultType)) { return; } var reference = MemenimScriptUtils.GetBindReference(value); reference.Value = defaultValue; }