本文整理汇总了C#中System.Type.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# Type.Equals方法的具体用法?C# Type.Equals怎么用?C# Type.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.Equals方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: getAppSetting
private static object getAppSetting(Type expectedType, string key)
{
string value = ConfigurationManager.AppSettings.Get(key);
if (value == null)
{
Log.Fatal("Configuration.cs", string.Format("AppSetting: {0} is not configured", key));
throw new Exception(string.Format("AppSetting: {0} is not configured.", key));
}
try
{
if (expectedType.Equals(typeof(int)))
{
return int.Parse(value);
}
if (expectedType.Equals(typeof(string)))
{
return value;
}
throw new Exception("Type not supported.");
}
catch (Exception ex)
{
throw new Exception(string.Format("Config key:{0} was expected to be of type {1} but was not.", key, expectedType),
ex);
}
}
示例2: Convert
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is BinomialNonInferiorityTesting == false)
{
throw new NotImplementedException();
}
if (targetType.Equals(typeof(IEnumerable)))
{
var values = new List<string>
{
Superiority,
SuperiorityWithMargin,
};
return values;
}
if (targetType.Equals(typeof(object)) || targetType.Equals(typeof(string)))
{
var binomialTesting = (BinomialNonInferiorityTesting)value;
switch (binomialTesting)
{
case BinomialNonInferiorityTesting.Superiority:
return Superiority;
case BinomialNonInferiorityTesting.SuperiorityWithMargin:
return SuperiorityWithMargin;
}
return binomialTesting.ToString();
}
throw new NotImplementedException("Unhandled targetType: " + targetType);
}
示例3: Convert
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is TimeToEventSpecification == false)
{
throw new NotImplementedException();
}
if (targetType.Equals(typeof(IEnumerable)))
{
var values = new List<string>
{
EventRate,
MedianTime,
};
return values;
}
if (targetType.Equals(typeof(object)) || targetType.Equals(typeof(string)))
{
var specification = (TimeToEventSpecification)value;
switch (specification)
{
case TimeToEventSpecification.EventRate:
return EventRate;
case TimeToEventSpecification.MedianTime:
return MedianTime;
}
return specification.ToString();
}
throw new NotImplementedException("Unhandled targetType: " + targetType);
}
示例4: GetService
public object GetService(Type serviceType)
{
_log.Debug("VerboseDependencyResolver.GetService(Type serviceType), serviceType: " + serviceType.ToString());
if(serviceType.Equals(typeof(IControllerFactory)))
{
// Cannot return controller here, as it is also set via
// ControllerBuilder.Current.SetControllerFactory.
//return new VerboseControllerFactory();
_log.Debug("VerboseDependencyResolver.GetService(Type serviceType) returning null.");
return null;
}
if (serviceType.Equals(typeof(IControllerActivator)))
{
_log.Debug("VerboseDependencyResolver.GetService(Type serviceType) returning null.");
return null;
}
if (typeof(IController).IsAssignableFrom(serviceType))
{
_log.Debug("VerboseDependencyResolver.GetService(Type serviceType) returning instance of HomeController.");
return new HomeController();
}
_log.Debug("VerboseDependencyResolver.GetService(Type serviceType) returning null.");
return null;
}
示例5: GetObjectValue
private static object GetObjectValue (string value, Type type)
{
if (type.IsEnum)
{
return GetEnumValue (type, value);
}
if (type.Equals ((typeof(bool))))
{
return GetBooleanValue (value);
}
if (type.Equals (typeof(DateTime)))
{
return GetDateTimeValue (value);
}
if (typeof(IConvertible).IsAssignableFrom (type))
{
return GetConvertibleValue (type, value);
}
string message = String.Format ("Unsupported type being used with NullableResolver: [{0}]", type.Name);
throw new Exception(message);
}
示例6: ConvertToType
/// <summary>
/// Converts the specified value to a value that fits the target type.
/// </summary>
/// <param name="targetType">The target <see cref="Type"/>.</param>
/// <param name="value">The value to convert.</param>
/// <returns>The converted value.</returns>
static object ConvertToType(Type targetType, object value)
{
if (targetType.IsAssignableFrom(value.GetType()))
return value;
if (targetType.IsEnum)
{
if (!Enum.IsDefined(targetType, value.ToString()))
throw new ScriptException(string.Format("The value '{0}' is not a member of {1}.", value, targetType));
return Enum.Parse(targetType, value.ToString(), true);
}
if (targetType.Equals(typeof(byte)))
return Convert.ToByte(value);
if (targetType.Equals(typeof(sbyte)))
return Convert.ToSByte(value);
if (targetType.Equals(typeof(short)))
return Convert.ToInt16(value);
if (targetType.Equals(typeof(ushort)))
return Convert.ToUInt16(value);
if (targetType.Equals(typeof(int)))
return Convert.ToInt32(value);
if (targetType.Equals(typeof(uint)))
return Convert.ToUInt32(value);
if (targetType.Equals(typeof(long)))
return Convert.ToInt64(value);
if (targetType.Equals(typeof(ulong)))
return Convert.ToUInt64(value);
throw new ScriptException(string.Format("Could not convert from {0} to {1}.", value.GetType(), targetType));
}
示例7: ApplyToFieldInfo
public override void ApplyToFieldInfo(FieldInfo Info, ISerializablePacket Packet, Type Field)
{
if (Field.Equals(typeof(byte[])))
Info.SetValue(Packet, val);
else if(Field.Equals(typeof(string)))
Info.SetValue(Packet,Marshal.ConvertToString((byte[])val));
}
示例8: ApplyToFieldInfo
public override void ApplyToFieldInfo(FieldInfo Info, ISerializablePacket Packet, Type Field)
{
if (Field.Equals(typeof(List<uint>)))
{
List<uint> Luint = new List<uint>();
foreach (ISerializableField Value in (List<ISerializableField>)val)
Luint.Add(Value.GetUint());
Info.SetValue(Packet, Luint);
}
else if(Field.Equals(typeof(List<ISerializablePacket>)))
{
List<ISerializablePacket> Packets = new List<ISerializablePacket>();
foreach (ISerializableField Value in (List<ISerializableField>)val)
Packets.Add(Value.GetPacket());
Info.SetValue(Packet, Packets);
}
else if (Field.Equals(typeof(List<bool>)))
{
List<bool> Bools = new List<bool>();
foreach (ISerializableField Value in (List<ISerializableField>)val)
Bools.Add((bool)Value.val);
Info.SetValue(Packet, Bools);
}
else if (Field.Equals(typeof(List<float>)))
{
List<float> floats = new List<float>();
foreach (ISerializableField Value in (List<ISerializableField>)val)
floats.Add(Value.GetFloat());
Info.SetValue(Packet, floats);
}
}
示例9: Convert
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is TimeToEventHypothesis == false)
{
throw new NotImplementedException();
}
if (targetType.Equals(typeof(IEnumerable)))
{
var values = new List<string>
{
RiskRatio,
RiskDifference,
};
return values;
}
if (targetType.Equals(typeof(object)) || targetType.Equals(typeof(string)))
{
var binomialTesting = (TimeToEventHypothesis)value;
switch (binomialTesting)
{
case TimeToEventHypothesis.RiskRatio:
return RiskRatio;
case TimeToEventHypothesis.RiskDifference:
return RiskDifference;
}
return binomialTesting.ToString();
}
throw new NotImplementedException("Unhandled targetType: " + targetType);
}
示例10: Distance
private int Distance (Type type_a, Type type_b) {
if (type_a.Equals (type_b))
return 0;
if (type_a.IsPrimitive && type_b.IsPrimitive &&
CanConvertPrimitive (type_a, type_b)) {
return 1;
}
int class_distance = 1;
Type base_a = type_a.BaseType;
while (base_a != null && !(base_a.Equals (typeof (object)))) {
if (type_b.Equals (base_a))
break;
// FIXME: this needs to check which interfaces bind tighter
Type[] interfaces = base_a.GetInterfaces ();
foreach (Type iface in interfaces) {
if (type_b.Equals (iface))
return 1;
}
class_distance++;
base_a = base_a.BaseType;
}
if (base_a == null || !(type_b.Equals (base_a))) {
class_distance = -1;
}
return class_distance;
}
示例11: Convert
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is TimeToEventAccrual == false)
{
throw new NotImplementedException();
}
if (targetType.Equals(typeof(IEnumerable)))
{
var values = new List<string>
{
Uniform,
Exponential,
};
return values;
}
if (targetType.Equals(typeof(object)) || targetType.Equals(typeof(string)))
{
var binomialTesting = (TimeToEventAccrual)value;
switch (binomialTesting)
{
case TimeToEventAccrual.Uniform:
return Uniform;
case TimeToEventAccrual.Exponential:
return Exponential;
}
return binomialTesting.ToString();
}
throw new NotImplementedException("Unhandled targetType: " + targetType);
}
示例12: GetConverter
public static Func<string, object> GetConverter(Type valueType)
{
if(valueType.Equals(typeof(string)))
{
return (s) => s;
}
if(valueType.Equals(typeof(int)))
{
return (s) => int.Parse(s);
}
else if(valueType.Equals(typeof(float)))
{
return (s) => float.Parse(s, CultureInfo.InvariantCulture.NumberFormat);
}
else if (valueType.Equals(typeof(double)))
{
return (s) => double.Parse(s, CultureInfo.InvariantCulture.NumberFormat);
}
else if(valueType.Equals(typeof(bool)))
{
return (s) => "true".Equals(s);
}
else if(valueType.IsEnum)
{
return (s) => Enum.Parse(valueType, s);
}
throw new NotImplementedException("Cannot create converter for " + valueType);
}
示例13: GetService
/// <summary>
/// Gets the service object of the specified type.
/// </summary>
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
/// <returns>
/// A service object of type serviceType.-or- null if there is no service object of type serviceType.
/// </returns>
public override object GetService(Type serviceType) {
if (serviceType.Equals(typeof(IMouseListener)))
return this;
else if (serviceType.Equals(typeof(IHoverListener)))
return this;
else
return null;
}
示例14: Convert
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return null;
if (!value.GetType().Equals(typeof (PlotType))) throw new ArgumentException();
if (targetType.Equals(typeof (int))) return (int) (PlotType) value;
if (targetType.Equals(typeof (object)) || targetType.Equals(typeof (string)))
{
var s = (PlotType) value;
switch (s)
{
case PlotType.Boundaries:
return Boundaries;
case PlotType.Power:
return Power;
case PlotType.TreatmentEffect:
return TreatmentEffect;
case PlotType.ConditionalPower:
return ConditionalPower;
case PlotType.SpendingFunction:
return SpendingFunction;
case PlotType.ExpectedSampleSize:
return ExpectedSampleSize;
case PlotType.BValues:
return BValues;
default:
return s.ToString();
}
}
if (targetType.Equals(typeof (IEnumerable)))
{
var values = new List<string>
{
Boundaries,
Power,
TreatmentEffect,
ConditionalPower,
SpendingFunction,
ExpectedSampleSize,
BValues,
};
return values;
}
throw new NotImplementedException();
}
示例15: CanConvertTo
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == null) { throw new ArgumentNullException("destinationType"); } // $NON-NLS-1
if (destinationType.Equals(typeof(string))
|| destinationType.Equals(typeof(Uri)))
return true;
return base.CanConvertTo(context, destinationType);
}