本文整理汇总了C#中System.Type.Implements方法的典型用法代码示例。如果您正苦于以下问题:C# Type.Implements方法的具体用法?C# Type.Implements怎么用?C# Type.Implements使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.Implements方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsCopyableCollectionType
private static bool IsCopyableCollectionType(Type type)
{
return type.IsArray ||
typeof(IList).IsAssignableFrom(type) ||
type.Implements(typeof(IList<>)) ||
typeof(IDictionary).IsAssignableFrom(type) ||
type.Implements(typeof(IDictionary<,>)) ||
type.Implements(typeof(ISet<>));
}
示例2: GetConversionType
public static BasicTypes GetConversionType(Type innType, Type outType)
{
if (innType.Implements<IDictionary>() && outType.Implements<IDictionary>())
return BasicTypes.Dictionary;
if (innType.Implements<IEnumerable>() && outType.Implements<IEnumerable>() && !innType.IsValueType())
return BasicTypes.List;
if (innType.IsValueType() && outType.IsValueType() || TypeExtensions.CanConvert(outType, innType))
return BasicTypes.Convertable;
return BasicTypes.ComplexType;
}
示例3: ApplicationComponentDependencyAttribute
public ApplicationComponentDependencyAttribute(Type dependency)
{
if (!dependency.Implements<IApplicationComponent>())
{
throw new InvalidOperationException("Application component dependency type must implement IApplicationComponent.");
}
}
示例4: Process
public void Process(Type type, PluginGraph graph)
{
if (!IsConcrete(type)) return;
if (type.Name.EndsWith("Actor") && type.Implements<AsyncHttpActor>())
graph.AddType(typeof(AsyncHttpActor), type);
}
示例5: EnsureType
protected static void EnsureType(Type implementationType, Type serviceType, bool assertImplementation = true)
{
if (assertImplementation && (!implementationType.IsClass || implementationType.IsAbstract))
throw new StyletIoCRegistrationException(String.Format("Type {0} is not a concrete class, and so can't be used to implemented service {1}", implementationType.GetDescription(), serviceType.GetDescription()));
// Test this first, as it's a bit clearer than hitting 'type doesn't implement service'
if (assertImplementation && implementationType.IsGenericTypeDefinition)
{
if (!serviceType.IsGenericTypeDefinition)
throw new StyletIoCRegistrationException(String.Format("You can't use an unbound generic type to implement anything that isn't an unbound generic service. Service: {0}, Type: {1}", serviceType.GetDescription(), implementationType.GetDescription()));
// This restriction may change when I figure out how to pass down the correct type argument
if (serviceType.GetTypeInfo().GenericTypeParameters.Length != implementationType.GetTypeInfo().GenericTypeParameters.Length)
throw new StyletIoCRegistrationException(String.Format("If you're registering an unbound generic type to an unbound generic service, both service and type must have the same number of type parameters. Service: {0}, Type: {1}", serviceType.GetDescription(), implementationType.GetDescription()));
}
else if (serviceType.IsGenericTypeDefinition)
{
if (implementationType.GetGenericArguments().Length > 0)
throw new StyletIoCRegistrationException(String.Format("You cannot bind the bound generic type {0} to the unbound generic service {1}", implementationType.GetDescription(), serviceType.GetDescription()));
else
throw new StyletIoCRegistrationException(String.Format("You cannot bind the non-generic type {0} to the unbound generic service {1}", implementationType.GetDescription(), serviceType.GetDescription()));
}
if (!implementationType.Implements(serviceType))
throw new StyletIoCRegistrationException(String.Format("Type {0} does not implement service {1}", implementationType.GetDescription(), serviceType.GetDescription()));
}
示例6: ErrorFilter
public ErrorFilter(Type exceptionType, IStringMatcher messageMatcher = null, IStringMatcher sourceMatcher = null, bool recurseInnerExceptions=false)
: base(messageMatcher,sourceMatcher)
{
if(exceptionType!=null && !exceptionType.Implements<Exception>()) throw new ArgumentException("The type must be an exception. It was: " + exceptionType, "exceptionType");
_exceptionType = exceptionType;
_recurseInnerExceptions = recurseInnerExceptions;
}
示例7: SpecificationAttribute
public SpecificationAttribute(Type specificationType)
{
if (!specificationType.Implements(typeof (ISpecification)))
{
throw new ValidationException("You must provide a valid specification type.");
}
SpecificationType = specificationType as ISpecification;
}
示例8: Materialize
public object Materialize(Type type)
{
if (type.Implements(typeof(IList<>)) == false) {
throw new NotImplementedException("Collection that does not implement IList<T> are not implemented yet.");
}
var argumentType = type.GetGenericArguments()[0];
var collection = (IList) Activator.CreateInstance(type);
foreach (var jsonObject in _jsonObjects) {
collection.Add(jsonObject.Materialize(argumentType));
}
return collection;
}
示例9: CheckRequiresReferenceHandling
internal static TypeErrorsBuilder CheckRequiresReferenceHandling(
this TypeErrorsBuilder typeErrors,
Type type,
MemberSettings settings,
Func<Type, bool> requiresReferenceHandling)
{
if (settings.ReferenceHandling == ReferenceHandling.Throw)
{
if (typeof(IEnumerable).IsAssignableFrom(type))
{
if (type.Implements(typeof(IDictionary<,>)))
{
var arguments = type.GetGenericArguments();
if (arguments.Length != 2 ||
requiresReferenceHandling(arguments[0]) ||
requiresReferenceHandling(arguments[1]))
{
typeErrors = typeErrors.CreateIfNull(type)
.Add(RequiresReferenceHandling.Enumerable);
}
}
else if (requiresReferenceHandling(type.GetItemType()))
{
typeErrors = typeErrors.CreateIfNull(type)
.Add(RequiresReferenceHandling.Enumerable);
}
}
else if (type.IsKeyValuePair())
{
var arguments = type.GetGenericArguments();
if (requiresReferenceHandling(arguments[0]) || requiresReferenceHandling(arguments[1]))
{
typeErrors = typeErrors.CreateIfNull(type)
.Add(RequiresReferenceHandling.ComplexType);
}
}
else if (requiresReferenceHandling(type))
{
typeErrors = typeErrors.CreateIfNull(type)
.Add(RequiresReferenceHandling.ComplexType);
}
}
return typeErrors;
}
示例10: CreateCommandParserFor
/// <summary>
/// Get the appropriate <see cref="ICommandParser"/> used by the <see cref="IEvent"/> of the passed type.
/// </summary>
/// <param name="type">Type of the <see cref="IEvent"/> to get the parser for.</param>
/// <returns>
/// <see cref="ICommandParser"/> for the <see cref="IEvent"/> of the passed type.
/// If the <see cref="IEvent"/> of the passed type dosen't provide a <see cref="ICommandParser"/> <c>null</c> is returned.
/// </returns>
public static ICommandParser CreateCommandParserFor(Type type)
{
if(type.Implements<IEvent>())
{
IEvent action = EventFactory.CreateEvent(type);
UsedCommandParser usedCommandParser = action.GetAttribute<UsedCommandParser>();
if(usedCommandParser != null)
{
Type commandParserType = usedCommandParser.ParserType;
object instance = Activator.CreateInstance(commandParserType);
if (instance.Implements<ICommandParser>())
{
return (ICommandParser)instance;
}
}
}
return null;
}
示例11: IsEquatableCore
protected static bool IsEquatableCore(Type type)
{
if (type == null)
{
return false;
}
bool result;
if (EquatableCheckedTypes.TryGetValue(type, out result))
{
return result;
}
if (type.IsNullable())
{
var underlyingType = Nullable.GetUnderlyingType(type);
result = IsEquatableCore(underlyingType);
EquatableCheckedTypes.TryAdd(type, result);
return result;
}
if (type.IsEnum)
{
result = true;
}
else if (type.IsImmutableArray())
{
// special casing ImmutableArray due to weird equality
// Implements IEquatable<ImmutableArray> but Equals does not compare elements.
result = false;
}
else
{
result = type.Implements(typeof(IEquatable<>), type);
}
EquatableCheckedTypes.TryAdd(type, result);
return result;
}
示例12: resolveOperatorType
protected override Type resolveOperatorType(Context ctx, Type leftType, Type rightType)
{
if (rightType == typeof(int))
{
// string repetition
if (leftType == typeof(string))
return typeof(string);
// array repetition
if (leftType.IsArray)
return leftType;
// typed sequence repetition
var enumerable = leftType.ResolveImplementationOf(typeof(IEnumerable<>));
if (enumerable != null)
return enumerable;
// untyped sequence repetition
if (leftType.Implements(typeof(IEnumerable), false))
return typeof(IEnumerable);
}
return null;
}
示例13: ToOrientValueString
public static string ToOrientValueString(this object value, Type targetType = null)
{
if (targetType == null)
{
if (value == null)
targetType = typeof(object);
else
targetType = value.GetType();
}
if (value == null && targetType == typeof(object))
return "null";
else if (targetType.IsEnum)
{
if (value == DBNull.Value) return "null";
return ((int)value).ToString();
}
else if (targetType == typeof(string))
{
if (value == DBNull.Value) return "null";
return string.Format("'{0}'", EscapeText((string)value));
}
else if (targetType == typeof(ulong))
{
if (value == DBNull.Value) return "null";
throw new NotSupportedException("Unsigned Long Integer values are not supported. Use either Decimal, Double or byte[] storage types.");
}
else if (targetType == typeof(DateTime))
{
if (value == DBNull.Value) return "null";
var dateString = ((DateTime)value).Kind == DateTimeKind.Utc ?
((DateTime)value).ToString("s").Replace('T', ' ')
: ((DateTime)value).ToUniversalTime().ToString("s").Replace('T', ' ');
return string.Format("'{0}'", dateString);
}
else if (targetType.IsArray)
{
if (value == DBNull.Value) return "null";
if (value == null)
{
return "null";
}
else if(((Array)value).Rank == 1)
{
if (((Array)value).GetType().GetElementType().IsValueType || ((Array)value).GetType().GetElementType() == typeof(string))
{
var converter = CreateByteConverter(((Array)value).GetType().GetElementType());
var count = ((Array)value).Length;
var type = GetElementType(((Array)value).GetType().GetElementType());
using (var stream = new MemoryStream())
{
stream.Write(BitConverter.GetBytes(count), 0, 4);
stream.Write(BitConverter.GetBytes(type), 0, 4); // this approach will only work for intrinsic value types
byte[] bytes;
foreach (var item in (Array)value)
{
bytes = converter(item);
stream.Write(bytes, 0, bytes.Length);
}
stream.Position = 0;
return string.Format("'{0}'", Convert.ToBase64String(stream.ToArray()));
}
}
else if (((Array)value).GetType().GetElementType().Implements<IIdentifiable>())
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ((Array)value).Length; i++)
{
var item = ((Array)value).GetValue(i) as IIdentifiable;
if (item != null && !string.IsNullOrEmpty(item.Id))
{
if (sb.Length > 0)
sb.Append(", ");
sb.Append(item.Id);
}
}
return sb.Length > 0 ? "[" + sb.ToString() + "]" : "null";
}
}
}
else if (targetType == typeof(decimal))
{
if (value == DBNull.Value) return "null";
var sValue = value.ToString();
if (!sValue.Contains("."))
sValue += ".";
return string.Format("'{0}'", sValue);
}
else if (targetType == typeof(double) || targetType == typeof(float))
{
if (value == DBNull.Value) return "null";
return string.Format("'{0}'", value.ToString()); // wrapping in quotes allows orient to parse exponential number formats
}
else if (targetType.IsValueType)
{
if (value == DBNull.Value) return "null";
return value.ToString();
}
else if (targetType.Implements<IIdentifiable>())
{
//.........这里部分代码省略.........
示例14: IsIEnumerableType
public static bool IsIEnumerableType(Type type)
{
return type.Implements(typeof(IEnumerable)) && type.IsInterface;
}
示例15: GetCollectionTCountProperty
private static PropertyInfo GetCollectionTCountProperty( Type targetType, Type elementType )
{
if ( !targetType.GetIsValueType() && targetType.Implements( typeof( ICollection<> ) ) )
{
return typeof( ICollection<> ).MakeGenericType( elementType ).GetProperty( "Count" );
}
var property = targetType.GetProperty( "Count" );
if ( property != null && property.PropertyType == typeof( int ) && property.GetIndexParameters().Length == 0 )
{
return property;
}
return null;
}