public static AbstractAccessObject Build(Type webao, IRequest req) { AddParameterAttribute[] parameters = (AddParameterAttribute[])webao.GetCustomAttributes(typeof(AddParameterAttribute), true); foreach (AddParameterAttribute param in parameters) { req.AddParameter(param.name, param.val); } BaseUrlAttribute baseURL = (BaseUrlAttribute)webao.GetCustomAttribute(typeof(BaseUrlAttribute), true); req.BaseUrl(baseURL.host); return((AbstractAccessObject)Activator.CreateInstance(webao, req)); }
// Example to how process custom attribute // But you should use a type cache to perform reflection // only once or minimize its use private void ProcessCustomAttributes() { // Get type Type type = typeof(WebaoArtist); // Get custom attribute BaseUrl // inherit parameter = false BaseUrlAttribute att = type.GetCustomAttribute <BaseUrlAttribute>(false); // When invoking GetCustomAttribute, the data from Assembly's Metadata // are deserialized and an object of type BaseUrlAttribute (attribute class) // is created. if (att != null) { string host = att.host; Console.WriteLine("host = " + host); } }