Exemple #1
0
        public static T LoadObjectDI <T>(String clrType)
        {
            var(assembly, type) = ClrHelpers.ParseClrType(clrType);
            var ass         = Assembly.Load(assembly);
            var handlerType = ass.GetType(type);
            var ctors       = handlerType.GetConstructors();

            if (ctors.Length != 1)
            {
                String errorMsg = $"Object '{type}' must have only one ctor";
                throw new ArgumentException(errorMsg);
            }
            var ctor       = ctors[0];
            var ctorParams = ctor.GetParameters();
            var prms       = new Object[ctorParams.Length];
            var currSL     = ServiceLocator.Current;

            for (int i = 0; i < ctorParams.Length; i++)
            {
                prms[i] = currSL.GetService(ctorParams[i].ParameterType);
            }
            var res = ctor.Invoke(prms);

            if (res is T resT)
            {
                return(resT);
            }
            return(default);
Exemple #2
0
        public static T LoadObjectSP <T>(String clrType)
        {
            var(assembly, type) = ClrHelpers.ParseClrType(clrType);
            var ass         = Assembly.Load(assembly);
            var handlerType = ass.GetType(type);
            var ctors       = handlerType.GetConstructors();

            if (ctors.Length != 1)
            {
                throw new ArgumentException($"Object '{type}' must have only one ctor");
            }
            var ctor   = ctors[0];
            var currSL = ServiceLocator.Current;
            var res    = ctor.Invoke(new Object[] { currSL });

            if (res is T resT)
            {
                return(resT);
            }
            throw new ArgumentException($"Object '{type}' does not implement {typeof(T).Name}");
        }