mirror of
https://github.com/zoriya/octokit.net.git
synced 2025-12-05 23:06:10 +00:00
72 lines
2.4 KiB
C#
72 lines
2.4 KiB
C#
using Octokit.Internal;
|
|
using Octokit.Reflection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace Octokit
|
|
{
|
|
internal static class ReflectionExtensions
|
|
{
|
|
public static string GetJsonFieldName(this MemberInfo memberInfo)
|
|
{
|
|
var memberName = memberInfo.Name;
|
|
var paramAttr = memberInfo.GetCustomAttribute<ParameterAttribute>();
|
|
|
|
if (paramAttr != null && !string.IsNullOrEmpty(paramAttr.Key))
|
|
{
|
|
memberName = paramAttr.Key;
|
|
}
|
|
|
|
return memberName.ToRubyCase();
|
|
}
|
|
|
|
public static IEnumerable<PropertyOrField> GetPropertiesAndFields(this Type type)
|
|
{
|
|
return ReflectionUtils.GetProperties(type).Select(property => new PropertyOrField(property))
|
|
.Union(ReflectionUtils.GetFields(type).Select(field => new PropertyOrField(field)))
|
|
.Where(p => (p.IsPublic || p.HasParameterAttribute) && !p.IsStatic);
|
|
}
|
|
|
|
public static bool IsDateTimeOffset(this Type type)
|
|
{
|
|
return type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?);
|
|
}
|
|
|
|
public static bool IsNullable(this Type type)
|
|
{
|
|
return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
|
|
}
|
|
|
|
public static IEnumerable<MemberInfo> GetMember(this Type type, string name)
|
|
{
|
|
return type.GetTypeInfo().DeclaredMembers.Where(m => m.Name == name);
|
|
}
|
|
|
|
public static PropertyInfo GetProperty(this Type t, string propertyName)
|
|
{
|
|
return t.GetTypeInfo().GetDeclaredProperty(propertyName);
|
|
}
|
|
|
|
public static bool IsAssignableFrom(this Type type, Type otherType)
|
|
{
|
|
return type.GetTypeInfo().IsAssignableFrom(otherType.GetTypeInfo());
|
|
}
|
|
public static IEnumerable<PropertyInfo> GetAllProperties(this Type type)
|
|
{
|
|
var typeInfo = type.GetTypeInfo();
|
|
var properties = typeInfo.DeclaredProperties;
|
|
|
|
var baseType = typeInfo.BaseType;
|
|
|
|
return baseType == null ? properties : properties.Concat(baseType.GetAllProperties());
|
|
}
|
|
|
|
public static bool IsEnumeration(this Type type)
|
|
{
|
|
return type.GetTypeInfo().IsEnum;
|
|
}
|
|
}
|
|
}
|