Esempio n. 1
0
        private IBaseObject TryToConstruct(Type t)
        {
            var c = new CtorDictionary();

            while (true)
            {
                try
                {
                    var res = Activator.CreateInstance(t, c);
                    return(res as IBaseObject);
                }
                catch (TargetInvocationException tex)
                {
                    var lenArgs = c.Count;
                    var ex      = tex.InnerException as ArgumentException;
                    if (ex == null)
                    {
                        throw new Exception($"for {t.Name} tex.InnerException is {tex.InnerException} ");
                    }
                    var name = ex.ParamName;
                    if (c.ContainsKey(name))
                    {
                        throw new Exception($"type {t} has {name} twice");
                    }
                    foreach (var ctor in t.GetConstructors())
                    {
                        var par = ctor.GetParameters().FirstOrDefault(p => p.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
                        if (par == null)
                        {
                            continue;
                        }
                        var def = GetDefault(par.ParameterType);
                        if (c.ContainsKey(name))
                        {
                            throw new Exception($"type {t} has {name} twice");
                        }
                        c.Add(name, def);
                        break;
                    }
                    if (c.Count != lenArgs)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(null);
        }
Esempio n. 2
0
        private CtorDictionary TryToConstruct(Type t)
        {
            if (!CanConstruct(t))
            {
                return(null);
            }
            CtorDictionary c = new CtorDictionary();

            while (true)
            {
                try
                {
                    var    c1  = new CtorDictionary(c);
                    object res = Activator.CreateInstance(t, c1);
                    if (c.Count() == 0)
                    {
                        //we need to add the arguments of a ctor
                        ConstructorInfo[] ctors = t.GetConstructors();
                        if (ctors.Any(it => it.GetParameters().Length == 0))
                        {
                            return(c);
                        }

                        foreach (ConstructorInfo ctor in ctors)
                        {
                            if (!ctor.IsPublic)
                            {
                                continue;
                            }

                            ParameterInfo[] pars = ctor.GetParameters();
                            if (pars[0].ParameterType == typeof(CtorDictionary))
                            {
                                continue;
                            }
                            //first constructor ok
                            foreach (ParameterInfo parm in pars)
                            {
                                c.Add(parm.Name, GetDefault(parm.ParameterType));
                            }
                            break;
                        }
                    }
                    return(c);
                }

                catch (TargetInvocationException tex)
                {
                    int    lenArgs    = c.Count;
                    string name       = null;
                    var    innerArgEx = tex.InnerException as ArgumentException;
                    if (innerArgEx != null)
                    {
                        name = innerArgEx.ParamName;
                    }
                    if (innerArgEx == null)
                    {
                        var innerKeyEx = tex.InnerException as KeyNotFoundException;
                        if (innerKeyEx != null)
                        {
                            // The given key 'nameColumn' was not present in the dictionary.
                            name = innerKeyEx.Message;
                            var first = name.IndexOf("'");
                            var last  = name.IndexOf("'", first + 1);
                            name = name.Substring(first + 1, last - first - 1);
                        }
                    }
                    if (string.IsNullOrWhiteSpace(name))
                    {
                        throw new Exception($"for {t.Name} tex.InnerException is {tex.InnerException} ");
                    }
                    if (c.ContainsKey(name))
                    {
                        throw new Exception($"type {t} has {name} twice");
                    }
                    foreach (ConstructorInfo ctor in t.GetConstructors())
                    {
                        ParameterInfo par = ctor.GetParameters().FirstOrDefault(p => p.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
                        if (par == null)
                        {
                            continue;
                        }
                        object def = GetDefault(par.ParameterType);
                        if (c.ContainsKey(name))
                        {
                            throw new Exception($"type {t} has {name} twice");
                        }
                        c.Add(name, def);
                        break;
                    }
                    if (c.Count != lenArgs)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //trying to find with the constructor - not ctor dictionary
            try
            {
                object res = Activator.CreateInstance(t, c.Values.Select(it => it).ToArray());
                return(c);
            }
            catch (Exception)
            {
                //do nothing - not successfull
                //trying to construct with first constructor that matches the values
                foreach (ConstructorInfo ctor in t.GetConstructors())
                {
                    List <ParameterInfo> pars = ctor.GetParameters().ToList();
                    int lenght = pars.Count;
                    pars.RemoveAll(it => c.ContainsKey(it.Name));
                    if (pars.Count == lenght)//not found arguments matching
                    {
                        continue;
                    }
                    CtorDictionary c1 = new CtorDictionary(c);
                    foreach (ParameterInfo par in pars)
                    {
                        object def = GetDefault(par.ParameterType);
                        c1[par.Name] = def;
                    }
                    try
                    {
                        object res = Activator.CreateInstance(t, c1.Values.Select(it => it).ToArray());
                        return(c1);
                    }
                    catch (Exception)
                    {
                        //DO NOTHING-CTOR NOT GOOD
                    }
                }
            }
            return(null);
        }