本文整理汇总了C#中System.Type.GetMember方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetMember方法的具体用法?C# Type.GetMember怎么用?C# Type.GetMember使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetMember方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ItemMetadata
/// <summary>
/// Creates a new ItemMetadta
/// </summary>
/// <param name="t"></param>
/// <param name="tableName"></param>
/// <param name="primaryName"></param>
/// <param name="secondaryName"></param>
public ItemMetadata(Type t, string tableName, string primaryName, string secondaryName)
{
Table = tableName;
Primary = primaryName;
Secondary = secondaryName;
HasSecondary = !string.IsNullOrEmpty(secondaryName);
ObjecType = t;
//
var p = ObjecType.GetMember(Primary).FirstOrDefault();
if (p == null)
throw new Exception("Invalid PrimaryKey");
if (p is FieldInfo)
{
PrimaryIsField = true;
PrimaryType = ((FieldInfo)p).FieldType;
}
else if (p is PropertyInfo)
{
PrimaryIsField = false;
PrimaryType = ((PropertyInfo)p).PropertyType;
}
else
throw new Exception("Invalid PrimaryKey");
if (!IsValidKeyType(PrimaryType))
throw new Exception("Invalid Key. Secondary key must be a string or a number.");
//
if (HasSecondary)
{
var s = ObjecType.GetMember(Secondary).FirstOrDefault();
if (s == null)
throw new Exception("Invalid SecondaryKey");
if (s is FieldInfo)
{
SecondaryIsField = true;
SecondaryType = ((FieldInfo)s).FieldType;
}
else if (s is PropertyInfo)
{
SecondaryIsField = false;
SecondaryType = ((PropertyInfo)s).PropertyType;
}
else
throw new Exception("Invalid SecondaryKey");
if (!IsValidKeyType(PrimaryType))
throw new Exception("Invalid Key. Secondary key must be a string or a number.");
}
}
示例2: AccessorPropertyMapper
public AccessorPropertyMapper(Type declaringType, string propertyName, Action<string> accesorValueSetter)
{
PropertyName = propertyName;
if (declaringType == null)
{
throw new ArgumentNullException("declaringType");
}
MemberInfo member = null;
if (propertyName != null)
{
member = declaringType.GetMember(propertyName, FieldBindingFlag).FirstOrDefault();
}
if (member == null)
{
accesorValueSetter("none");
canChangeAccessor = false;
}
else if ((member as FieldInfo) != null)
{
accesorValueSetter("field");
canChangeAccessor = false;
}
this.declaringType = declaringType;
this.propertyName = propertyName;
setAccessor = accesorValueSetter;
}
示例3: GetFastMethodInvoker
public Func<object, object[], object> GetFastMethodInvoker(Type declaringType, string memberName, Type[] typeParameters, Type[] argumentTypes, BindingFlags bindingFlags)
{
ArgumentUtility.CheckNotNull ("declaringType", declaringType);
ArgumentUtility.CheckNotNull ("memberName", memberName);
ArgumentUtility.CheckNotNull ("typeParameters", typeParameters);
ArgumentUtility.CheckNotNull ("argumentTypes", argumentTypes);
var overloads = (MethodBase[]) declaringType.GetMember (memberName, MemberTypes.Method, bindingFlags);
if (overloads.Length == 0)
throw new MissingMethodException (string.Format ("Method '{0}' not found on type '{1}'.", memberName, declaringType));
MethodInfo method;
if (typeParameters.Length > 0)
{
var methods = overloads.Where (m => m.GetGenericArguments ().Length == typeParameters.Length)
.Select (m => ((MethodInfo) m).MakeGenericMethod (typeParameters));
method = methods.SingleOrDefault (m => m.GetParameters ().Select (p => p.ParameterType).SequenceEqual (argumentTypes));
if (method == null)
throw new MissingMethodException (string.Format ("Generic overload of method '{0}`{1}' not found on type '{2}'.", memberName, typeParameters.Length, declaringType));
}
else
{
method = (MethodInfo) Type.DefaultBinder.SelectMethod (bindingFlags, overloads, argumentTypes, null);
if (method == null)
throw new MissingMethodException (string.Format ("Overload of method '{0}' not found on type '{1}'.", memberName, declaringType));
}
return CreateDelegateForMethod (method);
}
示例4: GetAddMethod
private static MethodInfo GetAddMethod(Type type, int paramCount, out bool hasMoreThanOne)
{
MethodInfo info = null;
MemberInfo[] infoArray = type.GetMember("Add", MemberTypes.Method, GetBindingFlags(type));
if (infoArray != null)
{
foreach (MemberInfo info2 in infoArray)
{
MethodInfo method = (MethodInfo) info2;
if (TypeReflector.IsPublicOrInternal(method))
{
ParameterInfo[] parameters = method.GetParameters();
if ((parameters != null) && (parameters.Length == paramCount))
{
if (info != null)
{
hasMoreThanOne = true;
return null;
}
info = method;
}
}
}
}
hasMoreThanOne = false;
return info;
}
示例5: AnalyzeInvokeAttribute
private void AnalyzeInvokeAttribute(RuleAnalysis analysis, Type contextType, Stack<MemberInfo> methodStack, CodeExpression targetExpression, RulePathQualifier targetQualifier, CodeExpressionCollection argumentExpressions, ParameterInfo[] parameters, List<CodeExpression> attributedExpressions)
{
foreach (MemberInfo info in contextType.GetMember(this.methodInvoked, MemberTypes.Property | MemberTypes.Method, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance))
{
if (!methodStack.Contains(info))
{
methodStack.Push(info);
object[] customAttributes = info.GetCustomAttributes(typeof(RuleAttribute), true);
if ((customAttributes != null) && (customAttributes.Length != 0))
{
foreach (RuleAttribute attribute in (RuleAttribute[]) customAttributes)
{
RuleReadWriteAttribute attribute2 = attribute as RuleReadWriteAttribute;
if (attribute2 != null)
{
attribute2.Analyze(analysis, info, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
}
else
{
((RuleInvokeAttribute) attribute).AnalyzeInvokeAttribute(analysis, contextType, methodStack, targetExpression, targetQualifier, argumentExpressions, parameters, attributedExpressions);
}
}
}
methodStack.Pop();
}
}
}
示例6: ShitFunc
// [JUNK]
void ShitFunc(Type T, string mtdName)
{
// T.InvokeMember(mtdName, BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, null);
RuntimeMethodHandle mtdHandle = ((MethodInfo)(T.GetMember(mtdName, BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public)[0])).MethodHandle;
MtdSig mtd = (MtdSig)Marshal.GetDelegateForFunctionPointer(mtdHandle.GetFunctionPointer(), typeof(MtdSig));
mtd();
}
示例7: IsMemberMarkedWithIgnoreAttribute
private static bool IsMemberMarkedWithIgnoreAttribute(PropertyInfo memberInfo, Type objectType)
{
var infos = typeof(INHibernateProxy).IsAssignableFrom(objectType) ?
objectType.BaseType.GetMember(memberInfo.Name) :
objectType.GetMember(memberInfo.Name);
return infos[0].GetCustomAttributes(typeof(JsonIgnoreAttribute), true).Length > 0;
}
示例8: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
foreach (string name in Enum.GetNames(objectType))
{
//get the attributes of the member
MemberInfo target = objectType.GetMember(name)[0];
object[] attributes = target.GetCustomAttributes(false);
foreach (object attribute in attributes)
{
RawValueAttribute rawValueAttribute;
string readerValue = reader.Value.ToString();
if ((rawValueAttribute = attribute as RawValueAttribute) != null && rawValueAttribute.RawValue == readerValue)
{
//serializer.Populate(reader, rawValueAttribute.RawValue);
return Enum.Parse(objectType, name);
}
}
}
}
throw new Exception("Could not match raw value");
}
示例9: RevertInfo
/// <summary>
/// Set up a revert info for a static object.
/// </summary>
/// <param name="monoBehaviour">The MonoBehaviour that is making this RevertInfo.</param>
/// <param name="type">The type of the static object</param>
/// <param name="memberName">The member name of the field/property/method to be called on revert.</param>
/// <param name="value">The current value you want to save.</param>
public RevertInfo(MonoBehaviour monoBehaviour, Type type, string memberName, object value)
{
this.MonoBehaviour = monoBehaviour;
this.Type = type;
this.value = value;
this.MemberInfo = Type.GetMember(memberName);
}
示例10: MakeAttribute
public Attribute MakeAttribute(Type type)
{
if (_func == null)
{
var ctors = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
var ctor = ctors.FirstOrDefault(c => c.GetParameters().Length == 0);
if (ctor != null)
{
var expr = Expression.Lambda<Func<Attribute>>(
Expression.Convert(
Expression.MemberInit(
Expression.New(ctor),
(IEnumerable<MemberBinding>)Values.Select(k =>
{
var member = type.GetMember(k.Key)[0];
var mtype = member.GetMemberType();
return Expression.Bind(
member,
Expression.Constant(Converter.ChangeType(k.Value, mtype), mtype));
})),
typeof(Attribute)));
_func = expr.Compile();
}
else
{
throw new NotImplementedException();
}
}
return _func();
}
示例11: GetExternalTypeMember
/// <summary>
/// Gets a see MemberAccess object that represents the instance, member name and other information on the member access expression.
/// </summary>
/// <param name="node">The Ast node associated with the member access operation</param>
/// <param name="type">The data type of the external object c#.</param>
/// <param name="varName">The name associated with the object. e.g. user.FirstName, "user".</param>
/// <param name="obj">The object on which the member access is being performed.</param>
/// <param name="memberName">The name of the member to get.</param>
/// <param name="isStatic">Whether or not this is a static access.</param>
/// <returns></returns>
public static MemberAccess GetExternalTypeMember(AstNode node, Type type, string varName, object obj, string memberName, bool isStatic)
{
// 1. Get all the members on the type that match the name.
var members = type.GetMember(memberName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.IgnoreCase);
// 2. Check that there were members with matching name.
if (members == null || members.Length == 0)
throw ExceptionHelper.BuildRunTimeException(node, "Property does not exist : '" + memberName + "' ");
// 3. Get the first member that matches the memberName.
var matchingMember = members[0];
var memberNameCaseIgnorant = matchingMember.Name;
var mode = isStatic ? MemberMode.CustObjMethodStatic : MemberMode.CustObjMethodInstance;
// 4. Store information about the member. instance, type, membername.
var member = new MemberAccess(mode);
member.Name = isStatic ? type.Name : varName;
member.DataType = type;
member.Instance = obj;
member.MemberName = matchingMember.Name;
// 1. Property.
if (matchingMember.MemberType == MemberTypes.Property)
member.Property = type.GetProperty(memberNameCaseIgnorant);
// 2. Method
else if (matchingMember.MemberType == MemberTypes.Method)
member.Method = type.GetMethod(matchingMember.Name);
else if (matchingMember.MemberType == MemberTypes.Field)
member.Field = type.GetField(matchingMember.Name);
return member;
}
示例12: FindIdMember
public string FindIdMember(
Type type
)
{
var memberInfo = type.GetMember(Name).SingleOrDefault(x => x.MemberType == MemberTypes.Field || x.MemberType == MemberTypes.Property);
return (memberInfo != null) ? Name : null;
}
示例13: getRoute
private Route getRoute(Type type)
{
Route route;
var routeMember = type.GetMember("Route", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase).SingleOrDefault();
if (routeMember != null)
{
var handler = HandlerFactory.Current.Create(new HandlerData { Type = type });
route = getExplicitRouteFromHandler(routeMember, handler) ?? new Route(getRouteUriByConvention(type));
if (route.RouteUri == "/")
{
route.RouteUri = String.Empty;
}
else if (route.RouteUri.IsEmpty())
{
route.RouteUri = getRouteUriByConvention(type);
}
}
else
{
route = new Route(getRouteUriByConvention(type));
}
route.RouteUri = addHandlerAreaToRouteUriIfRegistered(type, route.RouteUri);
return route;
}
示例14: GetColumn
/// <summary>
/// Provide a null value column by the specified type of <see cref="System.Type"/> and column name.
/// </summary>
/// <param name="type">The property value of <see cref="System.Type"/> used to create the <see cref="ColumnDescriber"/>.</param>
/// <param name="name">Column name use to create the <see cref="ColumnDescriber"/>.</param>
/// <returns>The expected <see cref="ColumnDescriber"/>.</returns>
/// <exception cref="System.ArgumentNullException">Name is null.</exception>
/// <exception cref="System.Reflection.TargetException">The object does not match the target type, or a property is an instance property but obj is null.</exception>
public static ColumnDescriber GetColumn(Type type, string name)
{
if (type == null) return ColumnDescriber.Empty;
ColumnDescriber describer = ColumnDescriber.Empty;
var memberInfos = type.GetMember(name, (BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy));
var mi = memberInfos.Length > 0 ? memberInfos[0] : null;
if (!ExcludeAttribute.IsExcluded(mi))
{
var propertyInfo = mi as PropertyInfo;
var fieldInfo = mi as FieldInfo;
if (propertyInfo != null)
{
describer = propertyInfo.GetValue(null, null) as ColumnDescriber;
}
else if (fieldInfo != null)
{
describer = fieldInfo.GetValue(null) as ColumnDescriber;
}
}
return describer;
}
示例15: GetDefaultMembers
private static MemberInfo[] GetDefaultMembers(Type typ, IReflect objIReflect, ref string DefaultName)
{
MemberInfo[] nonGenericMembers;
if (typ == objIReflect)
{
do
{
object[] customAttributes = typ.GetCustomAttributes(typeof(DefaultMemberAttribute), false);
if ((customAttributes != null) && (customAttributes.Length != 0))
{
DefaultName = ((DefaultMemberAttribute) customAttributes[0]).MemberName;
nonGenericMembers = GetNonGenericMembers(typ.GetMember(DefaultName, BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase));
if ((nonGenericMembers != null) && (nonGenericMembers.Length != 0))
{
return nonGenericMembers;
}
DefaultName = "";
return null;
}
typ = typ.BaseType;
}
while (typ != null);
DefaultName = "";
return null;
}
nonGenericMembers = GetNonGenericMembers(objIReflect.GetMember("", BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase));
if ((nonGenericMembers == null) || (nonGenericMembers.Length == 0))
{
DefaultName = "";
return null;
}
DefaultName = nonGenericMembers[0].Name;
return nonGenericMembers;
}