Server.Gumps.PropertiesGump.GetObjectFromString C# (CSharp) Méthode

GetObjectFromString() public static méthode

public static GetObjectFromString ( Type t, string s ) : object
t System.Type
s string
Résultat object
		public static object GetObjectFromString( Type t, string s )
		{
			if ( t == typeof( string ) )
			{
				return s;
			}
			else if ( t == typeof( byte ) || t == typeof( sbyte ) || t == typeof( short ) || t == typeof( ushort ) || t == typeof( int ) || t == typeof( uint ) || t == typeof( long ) || t == typeof( ulong ) )
			{
				if ( s.StartsWith( "0x" ) )
				{
					if ( t == typeof( ulong ) || t == typeof( uint ) || t == typeof( ushort ) || t == typeof( byte ) )
					{
						return Convert.ChangeType( Convert.ToUInt64( s.Substring( 2 ), 16 ), t );
					}
					else
					{
						return Convert.ChangeType( Convert.ToInt64( s.Substring( 2 ), 16 ), t );
					}
				}
				else
				{
					return Convert.ChangeType( s, t );
				}
			}
			else if ( t == typeof( double ) || t == typeof( float ) )
			{
				return Convert.ChangeType( s, t );
			}
			else if ( t.IsDefined( typeof( ParsableAttribute ), false ) )
			{
				MethodInfo parseMethod = t.GetMethod( "Parse", new Type[]{ typeof( string ) } );

				return parseMethod.Invoke( null, new object[]{ s } );
			}

			throw new Exception( "bad" );
		}

Usage Example

Exemple #1
0
        public override void OnResponse(NetState sender, RelayInfo info)
        {
            object toSet;
            bool   shouldSet, shouldSend = true;

            switch (info.ButtonID)
            {
            case 1:
            {
                TextRelay text = info.GetTextEntry(0);

                if (text != null)
                {
                    try
                    {
                        toSet     = PropertiesGump.GetObjectFromString(m_Property.PropertyType, text.Text);
                        shouldSet = true;
                    }
                    catch
                    {
                        toSet     = null;
                        shouldSet = false;
                        m_Mobile.SendMessage("Bad format");
                    }
                }
                else
                {
                    toSet     = null;
                    shouldSet = false;
                }

                break;
            }

            case 2:     // Null
            {
                toSet     = null;
                shouldSet = true;

                break;
            }

            case 3:     // Hue Picker
            {
                toSet      = null;
                shouldSet  = false;
                shouldSend = false;

                m_Mobile.SendHuePicker(new InternalPicker(m_Property, m_Mobile, m_Object, m_Stack, m_Page, m_List));

                break;
            }

            case 4:     // Body Picker
            {
                toSet      = null;
                shouldSet  = false;
                shouldSend = false;

                m_Mobile.SendGump(new SetBodyGump(m_Property, m_Mobile, m_Object, m_Stack, m_Page, m_List));

                break;
            }

            default:
            {
                toSet     = null;
                shouldSet = false;

                break;
            }
            }

            if (shouldSet)
            {
                try
                {
                    CommandLogging.LogChangeProperty(m_Mobile, m_Object, m_Property.Name, toSet == null ? "(null)" : toSet.ToString());
                    m_Property.SetValue(m_Object, toSet, null);
                    PropertiesGump.OnValueChanged(m_Object, m_Property, m_Stack);
                }
                catch
                {
                    m_Mobile.SendMessage("An exception was caught. The property may not have changed.");
                }
            }

            if (shouldSend)
            {
                m_Mobile.SendGump(new PropertiesGump(m_Mobile, m_Object, m_Stack, m_List, m_Page));
            }
        }