/// <summary> /// Tries to identify the type of credit card given it's number. /// </summary> /// <param name="number"></param> /// <returns></returns> public static CreditCardType IdentifyType(string number) { if (String.IsNullOrWhiteSpace(number)) { return(CreditCardType.Invalid); } if (CreditCardIdentificationRegexes == null) { var regexes = new Dictionary <CreditCardType, Regex>(); foreach (var member in typeof(CreditCardType).GetMembers()) { foreach (MetaDataAttribute rxattr in MetaDataAttribute.GetCustomAttributes(member, typeof(MetaDataAttribute)).Where(a => ((MetaDataAttribute)a).Name == "IdentificationRegex")) { regexes[(CreditCardType)Enum.Parse(typeof(CreditCardType), member.Name)] = new Regex((string)rxattr.Value, RegexOptions.Compiled); } } CreditCardIdentificationRegexes = regexes; } foreach (var pair in CreditCardIdentificationRegexes) { if (pair.Value.IsMatch(number.Replace(" ", "").ToUpperInvariant())) { return(pair.Key); } } return(CreditCardType.Unknown); }
XElement ToElement <TAttribute> (TypeDefinition type, string name, Func <TypeDefinition, TAttribute> parser, Func <TAttribute, XElement> toElement, Action <TAttribute, XElement> update) where TAttribute : class { TAttribute attr = parser(type); if (attr == null) { return(null); } IEnumerable <MetaDataAttribute> metadata = MetaDataAttribute.FromCustomAttributeProvider(type); IEnumerable <IntentFilterAttribute> intents = IntentFilterAttribute.FromTypeDefinition(type); XElement element = toElement(attr); if (element.Attribute(attName) == null) { element.Add(new XAttribute(attName, name)); } element.Add(metadata.Select(md => md.ToElement(PackageName))); element.Add(intents.Select(intent => intent.ToElement(PackageName))); if (update != null) { update(attr, element); } return(element); }
XElement ToProviderElement(TypeDefinition type, string name) { var attr = ContentProviderAttribute.FromTypeDefinition(type); if (attr == null) { return(null); } IEnumerable <MetaDataAttribute> metadata = MetaDataAttribute.FromCustomAttributeProvider(type); IEnumerable <GrantUriPermissionAttribute> grants = GrantUriPermissionAttribute.FromTypeDefinition(type); IEnumerable <IntentFilterAttribute> intents = IntentFilterAttribute.FromTypeDefinition(type); XElement element = attr.ToElement(PackageName); if (element.Attribute(attName) == null) { element.Add(new XAttribute(attName, name)); } element.Add(metadata.Select(md => md.ToElement(PackageName))); element.Add(grants.Select(intent => intent.ToElement(PackageName))); element.Add(intents.Select(intent => intent.ToElement(PackageName))); return(element); }
XElement CreateApplicationElement(XElement manifest, string applicationClass, List <TypeDefinition> subclasses) { var application = manifest.Descendants("application").FirstOrDefault(); List <ApplicationAttribute> assemblyAttr = Assemblies.Select(path => ApplicationAttribute.FromCustomAttributeProvider(Resolver.GetAssembly(path))) .Where(attr => attr != null) .ToList(); List <MetaDataAttribute> metadata = Assemblies.SelectMany(path => MetaDataAttribute.FromCustomAttributeProvider(Resolver.GetAssembly(path))) .Where(attr => attr != null) .ToList(); var usesLibraryAttr = Assemblies.SelectMany(path => UsesLibraryAttribute.FromCustomAttributeProvider(Resolver.GetAssembly(path))) .Where(attr => attr != null); var usesConfigurationAttr = Assemblies.SelectMany(path => UsesConfigurationAttribute.FromCustomAttributeProvider(Resolver.GetAssembly(path))) .Where(attr => attr != null); if (assemblyAttr.Count > 1) { throw new InvalidOperationException("There can be only one [assembly:Application] attribute defined."); } List <ApplicationAttribute> typeAttr = new List <ApplicationAttribute> (); List <UsesLibraryAttribute> typeUsesLibraryAttr = new List <UsesLibraryAttribute> (); List <UsesConfigurationAttribute> typeUsesConfigurationAttr = new List <UsesConfigurationAttribute> (); foreach (var t in subclasses) { ApplicationAttribute aa = ApplicationAttribute.FromCustomAttributeProvider(t); if (aa == null) { continue; } if (!t.IsSubclassOf("Android.App.Application")) { throw new InvalidOperationException(string.Format("Found [Application] on type {0}. [Application] can only be used on subclasses of Application.", t.FullName)); } typeAttr.Add(aa); metadata.AddRange(MetaDataAttribute.FromCustomAttributeProvider(t)); typeUsesLibraryAttr.AddRange(UsesLibraryAttribute.FromCustomAttributeProvider(t)); } if (typeAttr.Count > 1) { throw new InvalidOperationException("There can be only one type with an [Application] attribute; found: " + string.Join(", ", typeAttr.Select(aa => aa.Name).ToArray())); } if (assemblyAttr.Count > 0 && typeAttr.Count > 0) { throw new InvalidOperationException("Application cannot have both a type with an [Application] attribute and an [assembly:Application] attribute."); } ApplicationAttribute appAttr = assemblyAttr.SingleOrDefault() ?? typeAttr.SingleOrDefault(); var ull1 = usesLibraryAttr ?? new UsesLibraryAttribute [0]; var ull2 = typeUsesLibraryAttr.AsEnumerable() ?? new UsesLibraryAttribute [0]; var usesLibraryAttrs = ull1.Concat(ull2); var ucl1 = usesConfigurationAttr ?? new UsesConfigurationAttribute [0]; var ucl2 = typeUsesConfigurationAttr.AsEnumerable() ?? new UsesConfigurationAttribute [0]; var usesConfigurationattrs = ucl1.Concat(ucl2); bool needManifestAdd = true; if (appAttr != null) { var newapp = appAttr.ToElement(Resolver, PackageName); if (application == null) { application = newapp; } else { needManifestAdd = false; foreach (var n in newapp.Attributes()) { application.SetAttributeValue(n.Name, n.Value); } foreach (var n in newapp.Nodes()) { application.Add(n); } } } else if (application == null) { application = new XElement("application"); } else { needManifestAdd = false; } application.Add(metadata.Select(md => md.ToElement(PackageName))); if (needManifestAdd) { manifest.Add(application); } AddUsesLibraries(application, usesLibraryAttrs); AddUsesConfigurations(application, usesConfigurationattrs); if (applicationClass != null && application.Attribute(androidNs + "name") == null) { application.Add(new XAttribute(androidNs + "name", applicationClass)); } if (application.Attribute(androidNs + "allowBackup") == null) { application.Add(new XAttribute(androidNs + "allowBackup", "true")); } return(application); }