本文整理汇总了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");
}
}
}
示例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>();
}
}
示例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();
}
}
示例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);
}
}
}
示例5: GetTypeForObject
public virtual TypeInfo GetTypeForObject(Object value)
{
if (value == null)
throw new ArgumentNullException("value");
return MapType(value.GetType().GetTypeInfo());
}
示例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));
}
示例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
}
示例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;
}
示例9: Equals
public override bool Equals(Object obj)
{
if (!Object.ReferenceEquals(null, obj) && obj.GetType() == typeof(TerminalSymbol))
{
return Type == (TerminalSymbolType)obj;
}
return false;
}
示例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);
}
示例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;
}
示例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;
}
示例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);
}
示例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++;
}
}
示例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();
}