当前位置: 首页>>代码示例>>C#>>正文


C# Object.GetType方法代码示例

本文整理汇总了C#中Object.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Object.GetType方法的具体用法?C# Object.GetType怎么用?C# Object.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Object的用法示例。


在下文中一共展示了Object.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OnGUI

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        target = property.serializedObject.targetObject;
        MethodInfo[] mInfos = target.GetType().GetMethods();
        methodNames = new string[mInfos.Length];
        for(int i = 0; i < mInfos.Length; i++)
        { 
            methodNames[i] = mInfos[i].Name;
        }
        EditorGUI.BeginChangeCheck();
        index = EditorGUI.Popup(
                new Rect(position.position.x, position.position.y, position.width/2, 20),
                index,
                methodNames);

        //string value = EditorGUI.TextField(new Rect(position.position, new Vector2(position.size.x / 2 - 2, position.size.y)), property.FindPropertyRelative("MethodName").stringValue);
        if (EditorGUI.EndChangeCheck())
        { 
            property.FindPropertyRelative("MethodName").stringValue = methodNames[index];
        }

        if (GUI.Button(new Rect(new Vector2(position.position.x + position.width/2 + 2,position.position.y), new Vector2(position.size.x/2, position.size.y)), property.FindPropertyRelative("ButtonText").stringValue))
        {
            MethodInfo mInfo = target.GetType().GetMethod(property.FindPropertyRelative("MethodName").stringValue);
            if(mInfo != null)
            {
                mInfo.Invoke(target, null);
            }
            else
            {
                Debug.LogError("Method with name " + property.FindPropertyRelative("MethodName").stringValue + 
                    " was not found. Chec for spelling errors and make sure the desired method is public");
            }
        }
    }
开发者ID:mjholtzem,项目名称:Unity-SimpleInspectorButton,代码行数:35,代码来源:SimpleInspectorButtonDrawer.cs

示例2: SetTargetLight

 void SetTargetLight(Object obj)
 {
     if (obj.GetType() == typeof(Light))
     {
         targetLight = (Light)obj;
     } else if (obj.GetType() == typeof(GameObject))
     {
         targetLight = ((GameObject)obj).GetComponent<Light>();
     }
 }
开发者ID:Almax27,项目名称:LD33,代码行数:10,代码来源:GlobalLightController.cs

示例3: GetType_

 public void GetType_()
 {
     Object obj = new Object();
     foreach (var iteration in Benchmark.Iterations)
         using (iteration.StartMeasurement())
         {
             obj.GetType(); obj.GetType(); obj.GetType();
             obj.GetType(); obj.GetType(); obj.GetType();
             obj.GetType(); obj.GetType(); obj.GetType();
         }
 }
开发者ID:sky7sea,项目名称:corefx,代码行数:11,代码来源:Perf.Object.cs

示例4: WriteToBinaryStream

	/// <summary>
	/// Inspects all public primitive and serializable (non-reference) fields in the given object and writes them
	/// to the given BinaryWriter.
	/// </summary>
	/// <param name='type'>
	/// Type.
	/// </param>
	/// <param name='writer'>
	/// Writer.
	/// </param>
	public static void WriteToBinaryStream(Object targetObj, BinaryWriter writer) {
		System.Type objType = targetObj.GetType();
		
		FieldInfo[] publicFields = objType.GetFields(BindingFlags.Instance | BindingFlags.Public);		
		
		for(int i = 0; i < publicFields.Length; i++) {
			System.Type fieldType = publicFields[i].FieldType;

			if ((fieldType.IsValueType || fieldType == typeof(string)) && 
				!System.Attribute.IsDefined(publicFields[i], typeof(System.NonSerializedAttribute)) ) {

				Debug.Log(targetObj.name + " -> " + publicFields[i].Name + " = " + publicFields[i].GetValue(targetObj));
			}
		}
		
		Debug.Log("Public properties:");
		PropertyInfo[] publicProp = objType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
		
		for(int i = 0; i < publicProp.Length; i++) {
			System.Type propType = publicProp[i].PropertyType;
			
			if (propType.IsValueType || propType == typeof(string)) {
				
				Debug.Log(targetObj.name + " -> " + publicProp[i].Name + " = " + publicProp[i].GetValue(targetObj, null));
				Debug.Log(publicProp[i].Name + " = " + propType.Name);
			}
		}
	}
开发者ID:JulyMars,项目名称:frozen_free_fall,代码行数:38,代码来源:SerializerUtils.cs

示例5: GetTypeForObject

        public virtual TypeInfo GetTypeForObject(Object value)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            return MapType(value.GetType().GetTypeInfo());
        }
开发者ID:tijoytom,项目名称:corert,代码行数:7,代码来源:ReflectionContext.cs

示例6: GetSqlType

    /// <summary>
    /// Определяет SQL тип у значения параметра
    /// </summary>
    /// <param name="value">Значение параметра</param>
    /// <returns>Тип значения параметра</returns>
    public static SqlDbType GetSqlType(Object value)
    {
        if (value is SqlByte) return SqlDbType.TinyInt; // TinyInt
        if (value is SqlInt16) return SqlDbType.SmallInt; // SmallInt
        if (value is SqlInt32) return SqlDbType.Int; // Int32
        if (value is SqlInt64) return SqlDbType.BigInt;   // BigInt
        if (value is SqlBytes) return SqlDbType.VarBinary;  // Binary, VarBinary
        if (value is SqlBoolean) return SqlDbType.Bit;    // Bit
        if (value is SqlString) return SqlDbType.VarChar; // Char, NChar, VarChar, NVarChar

        if (value is DateTime && ((DateTime)value) == ((DateTime)value).Date) return SqlDbType.Date; // Date
        if (value is DateTime) return SqlDbType.DateTime2; // DateTime2
        if (value is SqlDateTime) return SqlDbType.DateTime; // DateTime, SmallDateTime
        if (value is TimeSpan) return SqlDbType.Time; // Time
        if (value is DateTimeOffset) return SqlDbType.DateTimeOffset; // DateTimeOffset ??? Не понятно как передать этот тип обратно на сервер ???

        if (value is SqlDecimal) return SqlDbType.Decimal; // Numeric, Decimal
        if (value is SqlDouble) return SqlDbType.Float; // Float
        if (value is SqlMoney) return SqlDbType.Money; // Money
        if (value is SqlSingle) return SqlDbType.Real; // Real
        if (value is SqlGuid) return SqlDbType.UniqueIdentifier; // UniqueIdentifier
        if (value is SqlXml) return SqlDbType.Xml; // UniqueIdentifier
        if (value is TParams) return SqlDbType.Udt; // Udt
        else
          throw new Exception(String.Format("Тип '{0}' не поддерживается средой CLR", value.GetType().Name));
    }
开发者ID:APouchkov,项目名称:ExtendedStoredProcedures,代码行数:31,代码来源:Sql.cs

示例7: Go

   public static void Go() {
      Console.WriteLine();			// Call a static method

      Object o = new Object();
      o.GetHashCode();				// Call a virtual instance method
      o.GetType();					// Call a nonvirtual instance method
   }
开发者ID:Helen1987,项目名称:edu,代码行数:7,代码来源:Ch06-1-TypeMembers.cs

示例8: IsValid

    public override Boolean IsValid(Object value)
    {
        Type objectType = value.GetType();
        //Get the property info for the object passed in.  This is the class the attribute is
        //  attached to
        //I would suggest caching this part... at least the PropertyInfo[]
        PropertyInfo[] neededProperties =
          objectType.GetProperties()
          .Where(propertyInfo => propertyInfo.Name == FirstPropertyName || propertyInfo.Name == SecondPropertyName)
          .ToArray();

        if (neededProperties.Count() != 2)
        {
            throw new ApplicationException("PropertiesMatchAttribute error on " + objectType.Name);
        }

        Boolean isValid = true;

        //Convert both values to string and compare...  Probably could be done better than this
        //  but let's not get bogged down with how dumb I am.  We should be concerned about
        //  dumb you are, jerkface.
        if (!Convert.ToString(neededProperties[0].GetValue(value, null)).Equals(Convert.ToString(neededProperties[1].GetValue(value, null))))
        {
            isValid = false;
        }

        return isValid;
    }
开发者ID:salez,项目名称:Guirotab,代码行数:28,代码来源:PropertiesMatchAttribute.cs

示例9: Equals

 public override bool Equals(Object obj)
 {
     if (!Object.ReferenceEquals(null, obj) && obj.GetType() == typeof(TerminalSymbol))
     {
         return Type == (TerminalSymbolType)obj;
     }
     return false;
 }
开发者ID:chrishenx,项目名称:math-expression-system,代码行数:8,代码来源:TerminalSymbol.cs

示例10: Equals

 public override bool Equals(Object o)
 {
     if (o == null) return false;
     if (o.GetType() != this.GetType()) return false;
     Point3 r = (Point3)o;
     if (this.z != r.z) return false;
     return base.Equals(o);
 }
开发者ID:rcorveira,项目名称:ave-2013-14-sem1,代码行数:8,代码来源:PointApp.cs

示例11: Equals

                public override bool Equals(Object obj)
                {
                    if (obj == null || GetType() != obj.GetType())
                        return false;

                    trainersRow Item = obj as trainersRow;
                    return Item.id == this.id;
                }
开发者ID:AndrianDTR,项目名称:Atlantic,代码行数:8,代码来源:Trainer.cs

示例12: Equals

                public override bool Equals(Object obj)
                {
                    if (obj == null || GetType() != obj.GetType())
                        return false;

                    scheduleRulesRow Item = obj as scheduleRulesRow;
                    return Item.id == this.id;
                }
开发者ID:AndrianDTR,项目名称:Atlantic,代码行数:8,代码来源:ScheduleRules.cs

示例13: CompareTo

 // Implementation of IComparable's CompareTo method 
 public Int32 CompareTo(Object o)
 {
     if (GetType() != o.GetType())
     {
         throw new ArgumentException("o is not a Point");
     }
     // Call type-safe CompareTo method 
     return CompareTo((Point)o);
 }
开发者ID:eric7237cire,项目名称:CodeJam,代码行数:10,代码来源:Boxing1.cs

示例14: ArgumentNullException

        public Object this[Object key] {
            get {
                if (key == null) {
                    throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key"));
                }
                Contract.EndContractBlock();
                DictionaryNode node = head;

                while (node != null) {
                    if ( node.key.Equals(key) ) {
                        return node.value;
                    }
                    node = node.next;
                }
                return null;
            }
            set {
                if (key == null) {
                    throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key"));
                }
                Contract.EndContractBlock();

#if FEATURE_SERIALIZATION
                if (!key.GetType().IsSerializable)                 
                    throw new ArgumentException(Environment.GetResourceString("Argument_NotSerializable"), "key");                    

                if( (value != null) && (!value.GetType().IsSerializable ) )
                    throw new ArgumentException(Environment.GetResourceString("Argument_NotSerializable"), "value");                    
#endif
                
                version++;
                DictionaryNode last = null;
                DictionaryNode node;
                for (node = head; node != null; node = node.next) {
                    if( node.key.Equals(key) ) {
                        break;
                    } 
                    last = node;
                }
                if (node != null) {
                    // Found it
                    node.value = value;
                    return;
                }
                // Not found, so add a new one
                DictionaryNode newNode = new DictionaryNode();
                newNode.key = key;
                newNode.value = value;
                if (last != null) {
                    last.next = newNode;
                }
                else {
                    head = newNode;
                }
                count++;
            }
        }
开发者ID:l1183479157,项目名称:coreclr,代码行数:57,代码来源:ListDictionaryInternal.cs

示例15: Serialize

    public static String Serialize(Object obj)
    {
        XmlSerializer xs = new XmlSerializer(obj.GetType());
        StringWriter sw = new StringWriter();
        sw.NewLine = "";

        xs.Serialize(sw, obj);
        return sw.ToString();
    }
开发者ID:neemask,项目名称:meta-core,代码行数:9,代码来源:Serializers.cs


注:本文中的Object.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。