Esempio n. 1
0
        public static bool TryParse <T>(this string text, out T value)
        {
            var type = typeof(T);

            if (SimpleType.IsSimpleType(type))
            {
                return(SimpleType.TryParse(text, out value));
            }

            value = default(T);

            if (!type.IsDefined(_ParsableAttribute, false))
            {
                return(false);
            }

            lock (_ParsableTryLock)
            {
                try
                {
                    _ParsableTryParams[1] = type;

                    var tryParse = type.GetMethod("TryParse", _ParsableTryParams);

                    _ParsableTryParams[1] = null;

                    _ParsableTryArgs[0] = text;
                    _ParsableTryArgs[1] = value;

                    var val = false;

                    if (tryParse != null)
                    {
                        val = (bool)tryParse.Invoke(null, _ParsableTryArgs);
                    }

                    value = (T)_ParsableTryArgs[1];

                    _ParsableTryArgs[0] = null;
                    _ParsableTryArgs[1] = null;

                    if (val)
                    {
                        return(true);
                    }
                }
                catch
                {
                    _ParsableTryArgs[0]   = null;
                    _ParsableTryArgs[1]   = null;
                    _ParsableTryParams[1] = null;
                }
            }

            lock (_ParsableLock)
            {
                try
                {
                    var parse = type.GetMethod("Parse", _ParsableParams);

                    _ParsableArgs[0] = text;

                    if (parse != null)
                    {
                        value = (T)parse.Invoke(null, _ParsableArgs);
                    }

                    _ParsableArgs[0] = null;

                    return(true);
                }
                catch
                {
                    _ParsableArgs[0] = null;
                }
            }

            value = default(T);

            return(false);
        }
Esempio n. 2
0
        public static string ResolveName(this Type t, params object[] args)
        {
            if (SimpleType.IsSimpleType(t))
            {
                return(FormatName(t.Name));
            }

            if (t.IsAbstract || !t.HasInterface <IEntity>())
            {
                return(FormatName(t.Name));
            }

            if (args.IsNullOrEmpty() ? !t.IsConstructable() : !t.IsConstructable(Type.GetTypeArray(args)))
            {
                return(FormatName(t.Name));
            }

            string value;

            if (_StringCache.TryGetValue(t, out value))
            {
                return(value);
            }

            value = String.Empty;

            var o = t.CreateInstanceSafe <IEntity>(args);

            if (o != null)
            {
                try
                {
                    if (o is Mobile)
                    {
                        value = ((Mobile)o).RawName;
                    }
                    else if (o is Item)
                    {
                        value = ((Item)o).ResolveName();
                    }
                    else
                    {
                        o.GetPropertyValue("Name", out value);
                    }
                }
                catch
                { }
                finally
                {
                    o.Delete();
                }
            }

            if (String.IsNullOrWhiteSpace(value))
            {
                value = FormatName(t.Name);
            }

            if (o == null || args.IsNullOrEmpty())
            {
                _StringCache[t] = value;
            }

            return(value);
        }