本文整理汇总了C#中System.Type.IsInstanceOfType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsInstanceOfType方法的具体用法?C# Type.IsInstanceOfType怎么用?C# Type.IsInstanceOfType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.IsInstanceOfType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetExtension
///////////////////////////////////////////////////////////////////////
/// <summary>Replaces or adds an extension of the given type
/// to the list.
///
/// This method looks for the first element on the list of the
/// given type and replaces it with the new value. If there are
/// no element of this type yet, a new element is added at the
/// end of the list.
/// </summary>
/// <param name="extensionElements">list of IExtensionElement</param>
/// <param name="type">type of the element to be added, removed
/// or replaced</param>
/// <param name="newValue">new value for this element, null to
/// remove it</param>
///////////////////////////////////////////////////////////////////////
public static void SetExtension(IList<IExtensionElementFactory> extensionElements,
Type type,
IExtensionElementFactory newValue)
{
int count = extensionElements.Count;
for (int i = 0; i < count; i++)
{
object element = extensionElements[i];
if (type.IsInstanceOfType(element))
{
if (newValue == null)
{
extensionElements.RemoveAt(i);
}
else
{
extensionElements[i] = newValue;
}
return;
}
}
if (newValue != null)
{
extensionElements.Add(newValue);
}
}
示例2: Annotation
public static object Annotation(object annotations, Type type)
{
Guard.NotNull(() => type, type);
if (annotations != null)
{
var annotationsArray = annotations as object[];
if (annotationsArray == null)
{
if (type.IsInstanceOfType(annotations))
return annotations;
}
else
{
for (int i = 0; i < annotationsArray.Length; i++)
{
var value = annotationsArray[i];
if (value == null)
break;
if (type.IsInstanceOfType(value))
return value;
}
}
}
return null;
}
示例3: ReadFile_
public static object ReadFile_(string file, Type expectedType, IProgressMonitor monitor)
{
bool clearLoadedPrjList = AlreadyLoadedPackages == null;
if (clearLoadedPrjList)
AlreadyLoadedPackages = new List<string> ();
if (AlreadyLoadedPackages.Contains (file))
return null;
AlreadyLoadedPackages.Add (file);
DubProject defaultPackage;
try{
using (var s = File.OpenText (file))
using (var r = new JsonTextReader (s))
defaultPackage = ReadPackageInformation(file, r, monitor);
}catch(Exception ex){
if (clearLoadedPrjList)
AlreadyLoadedPackages = null;
monitor.ReportError ("Couldn't load dub package \"" + file + "\"", ex);
return null;
}
if (expectedType.IsInstanceOfType (defaultPackage)) {
LoadDubProjectReferences (defaultPackage, monitor);
if (clearLoadedPrjList)
AlreadyLoadedPackages = null;
return defaultPackage;
}
var sln = new DubSolution();
if (!expectedType.IsInstanceOfType (sln)) {
if (clearLoadedPrjList)
AlreadyLoadedPackages = null;
return null;
}
sln.RootFolder.AddItem(defaultPackage, false);
sln.StartupItem = defaultPackage;
// Introduce solution configurations
foreach (var cfg in defaultPackage.Configurations)
sln.AddConfiguration(cfg.Name, false).Platform = cfg.Platform;
LoadDubProjectReferences (defaultPackage, monitor, sln);
sln.LoadUserProperties();
if (clearLoadedPrjList) {
AlreadyLoadedPackages = null;
// Clear 'dub list' outputs
DubReferencesCollection.DubListOutputs.Clear ();
}
return sln;
}
示例4: GetValue
public object GetValue(Type targetType)
{
var stringValue = _underlyingValue as string;
if (_underlyingValue == null)
{
if (targetType.IsValueType && !(targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
var valueAsString = string.IsNullOrEmpty(stringValue) ? "<null>" : string.Format("\"{0}\"", _underlyingValue);
throw new ArgumentException(string.Format("Cannot convert {0} to {1} (Column: '{2}', Row: {3})", valueAsString, targetType.Name, Header, Row));
}
return null;
}
ValueHasBeenUsed = true;
if (targetType.IsInstanceOfType(_underlyingValue))
return _underlyingValue;
if (targetType.IsEnum && _underlyingValue is string)
return Enum.Parse(targetType, (string)_underlyingValue);
if (targetType == typeof(DateTime))
return DateTime.Parse(stringValue);
try
{
return Convert.ChangeType(_underlyingValue, targetType);
}
catch (InvalidCastException ex)
{
throw new UnassignableExampleException(string.Format(
"{0} cannot be assigned to {1} (Column: '{2}', Row: {3})",
_underlyingValue == null ? "<null>" : _underlyingValue.ToString(),
targetType.Name, Header, Row), ex, this);
}
}
示例5: DeserializeCache
internal static StateFileBase DeserializeCache(string stateFile, TaskLoggingHelper log, Type requiredReturnType)
{
StateFileBase o = null;
try
{
if (((stateFile == null) || (stateFile.Length <= 0)) || !File.Exists(stateFile))
{
return o;
}
using (FileStream stream = new FileStream(stateFile, FileMode.Open))
{
object obj2 = new BinaryFormatter().Deserialize(stream);
o = obj2 as StateFileBase;
if ((o == null) && (obj2 != null))
{
log.LogMessageFromResources("General.CouldNotReadStateFileMessage", new object[] { stateFile, log.FormatResourceString("General.IncompatibleStateFileType", new object[0]) });
}
if ((o != null) && !requiredReturnType.IsInstanceOfType(o))
{
log.LogWarningWithCodeFromResources("General.CouldNotReadStateFile", new object[] { stateFile, log.FormatResourceString("General.IncompatibleStateFileType", new object[0]) });
o = null;
}
}
}
catch (Exception exception)
{
log.LogWarningWithCodeFromResources("General.CouldNotReadStateFile", new object[] { stateFile, exception.Message });
}
return o;
}
示例6: GetCustomAttribute
public object GetCustomAttribute(object obj, Type type, bool inherit)
{
foreach (object att in GetCustomAttributes (obj, type, inherit))
if (type.IsInstanceOfType (att))
return att;
return null;
}
示例7: GetService
public virtual object GetService (WorkspaceItem item, Type type)
{
if (type.IsInstanceOfType (this))
return this;
else
return GetNext (item).GetService (item, type);
}
示例8: GetAdapter
//IAdaptable
public virtual IAdaptable GetAdapter(Type adapter) {
#if UNITTEST
return adapter.IsInstanceOfType(this) ? this : null; //UnitTestではPoderosaの起動にならないケースもある
#else
return ProtocolsPlugin.Instance.PoderosaWorld.AdapterManager.GetAdapter(this, adapter);
#endif
}
示例9: SerializeRoot
public XElement SerializeRoot(object obj, Type rootType, XName name, XSerializerNamespaceCollection namespaces, SerializationScope globalScope)
{
Debug.Assert(rootType.IsInstanceOfType(obj));
Debug.Assert(namespaces != null);
var root = SerializeXElement(obj, rootType, name, globalScope);
//导入命名空间。
foreach (var ns in namespaces)
root.SetAttributeValue(XNamespace.Xmlns + ns.Prefix, ns.Uri);
//处理导入的类型。
var nsCounter = 0;
foreach (var descendant in root.Descendants())
{
var actualTypeName = descendant.Annotation<XName>();
if (actualTypeName != null)
{
if (actualTypeName.Namespace == descendant.GetDefaultNamespace())
{
descendant.SetAttributeValue(SerializationHelper.Xsi + "type", actualTypeName.LocalName);
}
else
{
var prefix = descendant.GetPrefixOfNamespace(actualTypeName.Namespace);
if (prefix == null)
{
nsCounter++;
prefix = "nss" + nsCounter;
descendant.SetAttributeValue(XNamespace.Xmlns + prefix, actualTypeName.NamespaceName);
}
descendant.SetAttributeValue(SerializationHelper.Xsi + "type", prefix + ":" + actualTypeName.LocalName);
}
descendant.RemoveAnnotations<XName>();
}
}
return root;
}
示例10: EncodeObject
static ByteString EncodeObject (object value, Type type, MemoryStream buffer, CodedOutputStream stream)
{
buffer.SetLength (0);
if (value != null && !type.IsInstanceOfType (value))
throw new ArgumentException ("Value of type " + value.GetType () + " cannot be encoded to type " + type);
if (value == null && !type.IsSubclassOf (typeof(RemoteObject)) && !IsACollectionType (type))
throw new ArgumentException ("null cannot be encoded to type " + type);
if (value == null)
stream.WriteUInt64 (0);
else if (value is Enum)
stream.WriteInt32 ((int)value);
else {
switch (Type.GetTypeCode (type)) {
case TypeCode.Int32:
stream.WriteInt32 ((int)value);
break;
case TypeCode.Int64:
stream.WriteInt64 ((long)value);
break;
case TypeCode.UInt32:
stream.WriteUInt32 ((uint)value);
break;
case TypeCode.UInt64:
stream.WriteUInt64 ((ulong)value);
break;
case TypeCode.Single:
stream.WriteFloat ((float)value);
break;
case TypeCode.Double:
stream.WriteDouble ((double)value);
break;
case TypeCode.Boolean:
stream.WriteBool ((bool)value);
break;
case TypeCode.String:
stream.WriteString ((string)value);
break;
default:
if (type.Equals (typeof(byte[])))
stream.WriteBytes (ByteString.CopyFrom ((byte[])value));
else if (IsAClassType (type))
stream.WriteUInt64 (((RemoteObject)value).id);
else if (IsAMessageType (type))
((IMessage)value).WriteTo (buffer);
else if (IsAListType (type))
WriteList (value, type, buffer);
else if (IsADictionaryType (type))
WriteDictionary (value, type, buffer);
else if (IsASetType (type))
WriteSet (value, type, buffer);
else if (IsATupleType (type))
WriteTuple (value, type, buffer);
else
throw new ArgumentException (type + " is not a serializable type");
break;
}
}
stream.Flush ();
return ByteString.CopyFrom (buffer.GetBuffer (), 0, (int)buffer.Length);
}
示例11: ChangeType
public static object ChangeType(object value, Type conversionType)
{
if (value == null)
{
return null;
}
Type valueType = value.GetType();
bool valueIsJava = valueType.IsSubclassOf(_javaObjectType) || valueType == _javaObjectType;
bool expectedIsJava = conversionType.IsSubclassOf(_javaObjectType) || conversionType == _javaObjectType;
if (expectedIsJava)
{
if (valueIsJava)
{
return value;
}
return value.WrapIntoJava();
}
if (valueIsJava)
{
object underlyingObject = (value as Object).ToManaged();
if (underlyingObject != null)
{
value = underlyingObject;
}
}
if (conversionType.IsInstanceOfType(value))
{
return value;
}
return Convert.ChangeType(value, conversionType);
}
示例12: ConvertImpl
/// <inheritdoc />
protected override object ConvertImpl(object sourceValue, Type targetType)
{
if (sourceValue == null || targetType.IsInstanceOfType(sourceValue))
return sourceValue;
throw new InvalidOperationException("The null converter does not support conversions.");
}
示例13: Add
public int Add(int jobId, object data, Type dataType)
{
if (!dataType.IsInstanceOfType(data))
throw new Exception("Incompatible type against data");
return AddInternal(jobId, data, dataType, DataEntryType.Direct);
}
示例14: KeepOriginal
static bool KeepOriginal(object arg, Type targetType)
{
return arg == null
|| targetType == typeof(object)
|| targetType.IsInstanceOfType(arg)
|| (targetType.IsEnum && arg.GetType() == typeof(int));
}
示例15: ChangeToCompatibleType
public static bool ChangeToCompatibleType(string value, Type destinationType, out object result)
{
if (string.IsNullOrEmpty(value))
{
result = null;
return false;
}
if (destinationType.IsInstanceOfType(value))
{
result = value;
return true;
}
try
{
result = TypeDescriptor.GetConverter(destinationType).ConvertFrom(value);
return true;
}
catch
{
result = null;
return false;
}
}