Example #1
0
        private static T GetImplementation <T>(
            ref T value)
            where T : class, IMemenimScriptBindable
        {
            if (value != null)
            {
                return(value);
            }

            var notImplementedType =
                MemenimScriptUtils.GetNotImplementedType(typeof(T));

            value = (T)Activator.CreateInstance(
                notImplementedType, true);

            return(value);
        }
        private IMemenimScriptBindable GetValue()
        {
            object value = _field
                           .GetValue(null);

            if (value != null)
            {
                return((IMemenimScriptBindable)value);
            }

            var notImplementedType = MemenimScriptUtils
                                     .GetNotImplementedType(Target);

            value = (IMemenimScriptBindable)Activator.CreateInstance(
                notImplementedType, true);

            return((IMemenimScriptBindable)value);
        }
Example #3
0
        private static (bool IsSuccess, IMemenimScriptBindable Implementation) TryGetImplementation(
            Type targetType, Type defaultType = null)
        {
            targetType = MemenimScriptUtils.GetBaseType(targetType);

            if (defaultType == targetType)
            {
                defaultType = null;
            }

            if (!typeof(IMemenimScriptBindable).IsAssignableFrom(targetType))
            {
                return(false, null);
            }

            if (defaultType != null && !targetType.IsAssignableFrom(defaultType))
            {
                return(false, null);
            }

            var notImplementedType = MemenimScriptUtils.GetNotImplementedType(targetType);
            var assemblies         = new List <Assembly>
            {
                Assembly.GetEntryAssembly(),
                Assembly.GetCallingAssembly(),
                Assembly.GetExecutingAssembly()
            };

            assemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies());

            foreach (var assembly in assemblies)
            {
                if (assembly == null)
                {
                    continue;
                }

                var types = assembly.GetTypes()
                            .Where(type => type.IsClass &&
                                   (targetType != null && targetType.IsAssignableFrom(type)) &&
                                   (type != targetType) &&
                                   (type != notImplementedType) &&
                                   (type != defaultType))
                            .ToArray();

                if (types.Length == 0)
                {
                    continue;
                }

                foreach (var type in types)
                {
                    IMemenimScriptBindable target;

                    try
                    {
                        target = (IMemenimScriptBindable)Activator.CreateInstance(
                            type, true);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    if (target == null)
                    {
                        continue;
                    }

                    return(true, target);
                }
            }

            return(false, null);
        }