本文整理汇总了C#中Type.IsInstanceOfType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsInstanceOfType方法的具体用法?C# Type.IsInstanceOfType怎么用?C# Type.IsInstanceOfType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.IsInstanceOfType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertSimpleType
private static object ConvertSimpleType(object value, Type destinationType, CultureInfo culture) {
if (value == null || destinationType.IsInstanceOfType(value)) {
return value;
}
// if this is a user-input value but the user didn't type anything, return no value
string valueAsString = value as string;
if (valueAsString != null && valueAsString.Trim().Length == 0) {
return null;
}
TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
bool canConvertFrom = converter.CanConvertFrom(value.GetType());
if (!canConvertFrom) {
converter = TypeDescriptor.GetConverter(value.GetType());
}
if (!(canConvertFrom || converter.CanConvertTo(destinationType))) {
string message = String.Format(CultureInfo.CurrentCulture, WebPageResources.HtmlHelper_NoConverterExists,
value.GetType().FullName, destinationType.FullName);
throw new InvalidOperationException(message);
}
try {
object convertedValue = (canConvertFrom) ?
converter.ConvertFrom(context: null, culture: culture, value: value) :
converter.ConvertTo(context: null, culture: culture, value: value, destinationType: destinationType);
return convertedValue;
}
catch (Exception ex) {
string message = String.Format(CultureInfo.CurrentUICulture, WebPageResources.HtmlHelper_ConversionThrew,
value.GetType().FullName, destinationType.FullName);
throw new InvalidOperationException(message, ex);
}
}
示例2: AssignableMatch
private static bool AssignableMatch(DataRow row, Type connectionType)
{
DebugCheck.NotNull(row);
DebugCheck.NotNull(connectionType);
return connectionType.IsInstanceOfType(DbProviderFactories.GetFactory(row).CreateConnection());
}
示例3: Find
public object Find (Type type)
{
foreach (object value in List)
if (type.IsInstanceOfType (value))
return value;
return null;
}
示例4: ExpressionEvaluationCompiled
public void ExpressionEvaluationCompiled(Expression expression, Type type, bool useInterpreter)
{
bool expected = expression.Type == typeof(void)
? type == typeof(void)
: type.IsInstanceOfType(Expression.Lambda<Func<object>>(Expression.Convert(expression, typeof(object))).Compile()());
Assert.Equal(expected, Expression.Lambda<Func<bool>>(Expression.TypeIs(expression, type)).Compile(useInterpreter)());
}
示例5: ConvertSimpleType
private static object ConvertSimpleType(CultureInfo culture, object value, Type destinationType)
{
if (value == null || destinationType.IsInstanceOfType(value))
{
return value;
}
// if this is a user-input value but the user didn't type anything, return no value
string valueAsString = value as string;
if (valueAsString != null && valueAsString.Trim().Length == 0)
{
return null;
}
TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
bool canConvertFrom = converter.CanConvertFrom(value.GetType());
if (!canConvertFrom)
{
converter = TypeDescriptor.GetConverter(value.GetType());
}
if (!(canConvertFrom || converter.CanConvertTo(destinationType)))
{
// EnumConverter cannot convert integer, so we verify manually
if (destinationType.IsEnum && value is int)
{
return Enum.ToObject(destinationType, (int)value);
}
// In case of a Nullable object, we try again with its underlying type.
Type underlyingType = Nullable.GetUnderlyingType(destinationType);
if (underlyingType != null)
{
return ConvertSimpleType(culture, value, underlyingType);
}
string message = String.Format(CultureInfo.CurrentCulture, MvcResources.ValueProviderResult_NoConverterExists,
value.GetType().FullName, destinationType.FullName);
throw new InvalidOperationException(message);
}
try
{
object convertedValue = (canConvertFrom)
? converter.ConvertFrom(null /* context */, culture, value)
: converter.ConvertTo(null /* context */, culture, value, destinationType);
return convertedValue;
}
catch (Exception ex)
{
string message = String.Format(CultureInfo.CurrentCulture, MvcResources.ValueProviderResult_ConversionThrew,
value.GetType().FullName, destinationType.FullName);
throw new InvalidOperationException(message, ex);
}
}
示例6: ConvertSimpleType
private static object ConvertSimpleType(CultureInfo culture, object value, Type destinationType)
{
if (value == null || destinationType.IsInstanceOfType(value))
{
return value;
}
// if this is a user-input value but the user didn't type anything, return no value
string valueAsString = value as string;
if (valueAsString != null && String.IsNullOrWhiteSpace(valueAsString))
{
return null;
}
TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
bool canConvertFrom = converter.CanConvertFrom(value.GetType());
if (!canConvertFrom)
{
converter = TypeDescriptor.GetConverter(value.GetType());
}
if (!(canConvertFrom || converter.CanConvertTo(destinationType)))
{
// EnumConverter cannot convert integer, so we verify manually
if (destinationType.IsEnum && value is int)
{
return Enum.ToObject(destinationType, (int)value);
}
// In case of a Nullable object, we try again with its underlying type.
Type underlyingType = Nullable.GetUnderlyingType(destinationType);
if (underlyingType != null)
{
return ConvertSimpleType(culture, value, underlyingType);
}
throw Error.InvalidOperation(SRResources.ValueProviderResult_NoConverterExists, value.GetType(), destinationType);
}
try
{
return canConvertFrom
? converter.ConvertFrom(null, culture, value)
: converter.ConvertTo(null, culture, value, destinationType);
}
catch (Exception ex)
{
throw Error.InvalidOperation(ex, SRResources.ValueProviderResult_ConversionThrew, value.GetType(), destinationType);
}
}
示例7: UnwrapPossibleArrayType
private static object UnwrapPossibleArrayType(object value, Type destinationType, CultureInfo culture)
{
if (value == null || destinationType.IsInstanceOfType(value))
{
return value;
}
// array conversion results in four cases, as below
Array valueAsArray = value as Array;
if (destinationType.IsArray)
{
Type destinationElementType = destinationType.GetElementType();
if (valueAsArray != null)
{
// case 1: both destination + source type are arrays, so convert each element
IList converted = Array.CreateInstance(destinationElementType, valueAsArray.Length);
for (int i = 0; i < valueAsArray.Length; i++)
{
converted[i] = ConvertSimpleType(valueAsArray.GetValue(i), destinationElementType, culture);
}
return converted;
}
else
{
// case 2: destination type is array but source is single element, so wrap element in array + convert
object element = ConvertSimpleType(value, destinationElementType, culture);
IList converted = Array.CreateInstance(destinationElementType, 1);
converted[0] = element;
return converted;
}
}
else if (valueAsArray != null)
{
// case 3: destination type is single element but source is array, so extract first element + convert
if (valueAsArray.Length > 0)
{
value = valueAsArray.GetValue(0);
return ConvertSimpleType(value, destinationType, culture);
}
else
{
// case 3(a): source is empty array, so can't perform conversion
return null;
}
}
// case 4: both destination + source type are single elements, so convert
return ConvertSimpleType(value, destinationType, culture);
}
示例8: ConvertSimpleType
private static object ConvertSimpleType(CultureInfo culture, object value, Type destinationType)
{
if (value == null || destinationType.IsInstanceOfType(value))
{
return value;
}
// if this is a user-input value but the user didn't type anything, return no value
string valueAsString = value as string;
if (valueAsString != null && String.IsNullOrWhiteSpace(valueAsString))
{
return null;
}
// In case of a Nullable object, we extract the underlying type and try to convert it.
Type underlyingType = Nullable.GetUnderlyingType(destinationType);
if (underlyingType != null)
{
destinationType = underlyingType;
}
// String doesn't provide convertibles to interesting types, and thus it will typically throw rather than succeed.
if (valueAsString == null)
{
// If the source type implements IConvertible, try that first
IConvertible convertible = value as IConvertible;
if (convertible != null)
{
try
{
return convertible.ToType(destinationType, culture);
}
catch
{
}
}
}
// Last resort, look for a type converter
TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
bool canConvertFrom = converter.CanConvertFrom(value.GetType());
if (!canConvertFrom)
{
converter = TypeDescriptor.GetConverter(value.GetType());
}
if (!(canConvertFrom || converter.CanConvertTo(destinationType)))
{
// EnumConverter cannot convert integer, so we verify manually
if (destinationType.IsEnum && value is int)
{
return Enum.ToObject(destinationType, (int)value);
}
string message = String.Format(CultureInfo.CurrentCulture, MvcResources.ValueProviderResult_NoConverterExists,
value.GetType().FullName, destinationType.FullName);
throw new InvalidOperationException(message);
}
try
{
object convertedValue = (canConvertFrom)
? converter.ConvertFrom(null /* context */, culture, value)
: converter.ConvertTo(null /* context */, culture, value, destinationType);
return convertedValue;
}
catch (Exception ex)
{
string message = String.Format(CultureInfo.CurrentCulture, MvcResources.ValueProviderResult_ConversionThrew,
value.GetType().FullName, destinationType.FullName);
throw new InvalidOperationException(message, ex);
}
}
示例9: TryConvertToType
object TryConvertToType (object value, Type type, ref bool failed)
{
if (type.IsInstanceOfType (value)) {
return value;
}
if (type.IsByRef) {
var elementType = type.GetElementType ();
if (value == null || elementType.IsInstanceOfType (value)) {
return value;
}
}
if (value == null)
return value;
if (type.IsEnum) {
type = Enum.GetUnderlyingType (type);
if (type == value.GetType ())
return value;
}
if (type.IsPrimitive) {
var res = IsConvertibleToPrimitiveType (value, type);
if (res != null)
return res;
} else if (type.IsPointer) {
var vtype = value.GetType ();
if (vtype == typeof (IntPtr) || vtype == typeof (UIntPtr))
return value;
}
failed = true;
return null;
}
示例10: GetReferences
public object[] GetReferences(Type baseType)
{
object[] references = GetReferences();
List<object> filtered = new List<object>();
foreach (object reference in references)
{
if (baseType.IsInstanceOfType(reference))
filtered.Add(reference);
}
return filtered.ToArray();
}
示例11: ConnectionPointCookie
/// <include file='doc\NativeMethods.uex' path='docs/doc[@for="NativeMethods.ConnectionPointCookie.ConnectionPointCookie1"]/*' />
/// <devdoc>
/// Creates a connection point to of the given interface type.
/// which will call on a managed code sink that implements that interface.
/// </devdoc>
public ConnectionPointCookie(object source, object sink, Type eventInterface, bool throwException, out bool connected){
connected = false;
Exception ex = null;
if (source is UnsafeNativeMethods.IConnectionPointContainer) {
UnsafeNativeMethods.IConnectionPointContainer cpc = (UnsafeNativeMethods.IConnectionPointContainer)source;
try {
Guid tmp = eventInterface.GUID;
if (cpc.FindConnectionPoint(ref tmp, out connectionPoint) != NativeMethods.S_OK) {
connectionPoint = null;
}
}
catch (Exception) {
connectionPoint = null;
}
if (connectionPoint == null) {
ex = new ArgumentException(SR.GetString(SR.ConnPointSourceIF, eventInterface.Name ));
}
else if (sink == null || !eventInterface.IsInstanceOfType(sink)) {
ex = new InvalidCastException(SR.GetString(SR.ConnPointSinkIF));
}
else {
int hr = connectionPoint.Advise(sink, ref cookie);
if (hr != S_OK) {
cookie = 0;
Marshal.ReleaseComObject(connectionPoint);
connectionPoint = null;
ex = new ExternalException(SR.GetString(SR.ConnPointAdviseFailed, eventInterface.Name, hr ));
}
else {
connected = true;
}
}
}
else {
ex = new InvalidCastException(SR.GetString(SR.ConnPointSourceIF, "IConnectionPointContainer"));
}
if (throwException && (connectionPoint == null || cookie == 0)) {
if (connectionPoint != null) {
Marshal.ReleaseComObject(connectionPoint);
}
if (ex == null) {
throw new ArgumentException(SR.GetString(SR.ConnPointCouldNotCreate, eventInterface.Name ));
}
else {
throw ex;
}
}
#if DEBUG
new EnvironmentPermission(PermissionState.Unrestricted).Assert();
try {
callStack = Environment.StackTrace;
}
finally {
System.Security.CodeAccessPermission.RevertAssert();
}
#endif
}
示例12: ConvertPrimitiveValue
public static object ConvertPrimitiveValue(object value, Type type)
{
Contract.Assert(value != null);
Contract.Assert(type != null);
// if value is of the same type nothing to do here.
if (value.GetType() == type || value.GetType() == Nullable.GetUnderlyingType(type))
{
return value;
}
if (type.IsInstanceOfType(value))
{
return value;
}
string str = value as string;
if (type == typeof(char))
{
if (str == null || str.Length != 1)
{
throw new ValidationException(Error.Format(SRResources.PropertyMustBeStringLengthOne));
}
return str[0];
}
else if (type == typeof(char?))
{
if (str == null || str.Length > 1)
{
throw new ValidationException(Error.Format(SRResources.PropertyMustBeStringMaxLengthOne));
}
return str.Length > 0 ? str[0] : (char?)null;
}
else if (type == typeof(char[]))
{
if (str == null)
{
throw new ValidationException(Error.Format(SRResources.PropertyMustBeString));
}
return str.ToCharArray();
}
else if (type == typeof(Binary))
{
return new Binary((byte[])value);
}
else if (type == typeof(XElement))
{
if (str == null)
{
throw new ValidationException(Error.Format(SRResources.PropertyMustBeString));
}
return XElement.Parse(str);
}
else
{
type = Nullable.GetUnderlyingType(type) ?? type;
if (type.IsEnum)
{
if (str == null)
{
throw new ValidationException(Error.Format(SRResources.PropertyMustBeString));
}
return Enum.Parse(type, str);
}
else if (type == typeof(DateTime))
{
if (value is DateTimeOffset)
{
DateTimeOffset dateTimeOffsetValue = (DateTimeOffset)value;
TimeZoneInfo timeZone = TimeZoneInfoHelper.TimeZone;
dateTimeOffsetValue = TimeZoneInfo.ConvertTime(dateTimeOffsetValue, timeZone);
return dateTimeOffsetValue.DateTime;
}
if (value is Date)
{
Date dt = (Date)value;
return (DateTime)dt;
}
throw new ValidationException(Error.Format(SRResources.PropertyMustBeDateTimeOffsetOrDate));
}
else if (type == typeof(TimeSpan))
{
if (value is TimeOfDay)
{
TimeOfDay tod = (TimeOfDay)value;
return (TimeSpan)tod;
}
throw new ValidationException(Error.Format(SRResources.PropertyMustBeTimeOfDay));
}
else
{
//.........这里部分代码省略.........
示例13: FindAll
public object[] FindAll (Type type)
{
ArrayList searchResults = new ArrayList ();
foreach (object value in List)
if (type.IsInstanceOfType(value))
searchResults.Add (value);
object[] returnValue = new object [searchResults.Count];
if (searchResults.Count > 0)
searchResults.CopyTo (returnValue);
return returnValue;
}
示例14: ConvertData
private object ConvertData (object data, Type data_type)
{
if (data == null)
return null;
TypeConverter converter = TypeDescriptor.GetConverter (data.GetType ());
if (converter != null && converter.CanConvertTo (data_type))
return converter.ConvertTo (data, data_type);
converter = TypeDescriptor.GetConverter (data_type);
if (converter != null && converter.CanConvertFrom (data.GetType()))
return converter.ConvertFrom (data);
if (data is IConvertible) {
object res = Convert.ChangeType (data, data_type);
if (data_type.IsInstanceOfType (res))
return res;
}
return null;
}
示例15: ConvertSimpleType
static object ConvertSimpleType(CultureInfo culture, object value, Type destinationType)
{
if (value == null || destinationType.IsInstanceOfType(value))
{
return value;
}
// if this is a user-input value but the user didn't type anything, return no value
var valueAsString = value as string;
if (valueAsString != null && valueAsString.Length == 0)
{
return null;
}
var converter = TypeDescriptor.GetConverter(destinationType);
var canConvertFrom = converter.CanConvertFrom(value.GetType());
if (!canConvertFrom)
{
converter = TypeDescriptor.GetConverter(value.GetType());
}
if (!(canConvertFrom || converter.CanConvertTo(destinationType)))
{
var message = String.Format(CultureInfo.CurrentUICulture, MvcResources.ValueProviderResult_NoConverterExists,
value.GetType().FullName, destinationType.FullName);
throw new InvalidOperationException(message);
}
try
{
var convertedValue = (canConvertFrom)
?
converter.ConvertFrom(null /* context */, culture, value)
:
converter.ConvertTo(null /* context */, culture, value, destinationType);
return convertedValue;
}
catch (Exception ex)
{
var message = String.Format(CultureInfo.CurrentUICulture, MvcResources.ValueProviderResult_ConversionThrew,
value.GetType().FullName, destinationType.FullName);
throw new InvalidOperationException(message, ex);
}
}