Is there a URL builder that supports request parameter concatenation as well? - asp.net

I want to achieve something like the following:
UrlBuilder ub = new UrlBuilder("http://www.google.com/search");
ub.Parameters.Add("q","request");
ub.Parameters.Add("sourceid","ie8");
string uri = ub.ToString(); //http://www.google.com/search?q=request&sourceid=ie8
Is there anything in .NET, or I will have to create my own?

Nothing exists that I know of. Here's something simple which does what you want. Usage would be:
UrlBuilder ub = new UrlBuilder("www.google.com/search")
.AddQuery("q", "request")
.AddQuery("sourceid", "ie8");
string url=ub.ToString();
==
Code is:
public class UrlBuilder
{
private string _authority;
private string _host;
private int? _port;
private Dictionary<string, object> _query = new Dictionary<string, object>();
public UrlBuilder(string host)
: this("http", host, null)
{
}
public UrlBuilder(string authority, string host)
: this(authority, host, null)
{
}
public UrlBuilder(string authority, string host, int? port)
{
this._authority = authority;
this._host = host;
this._port = port;
}
public UrlBuilder AddQuery(string key, object value)
{
this._query.Add(key, value);
return this;
}
public override string ToString()
{
string url = _authority + "://" + _host;
if (_port.HasValue)
{
url += ":" + _port.ToString();
}
return AppendQuery(url);
}
private string AppendQuery(string url)
{
if (_query.Count == 0)
{
return url;
}
url += "?";
bool isNotFirst = false;
foreach (var key in this._query.Keys)
{
if (isNotFirst)
{
url += "&";
}
url += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(this._query[key].ToString());
isNotFirst = true;
}
return url;
}
}
}

Does the UriBuilder class help?
It doesn't have any method to add querystring parameters. Look at Query property to set values.
EDIT: See UriTemplate class.

I developed my own, that's more suitable for my needs, thanks for your code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
namespace Utils
{
public class ParameterCollectionBuilder : NameValueCollection
{
#region NameValueCollection Implementation
public ParameterCollectionBuilder() : base() { }
public ParameterCollectionBuilder(string uri)
{
Init(uri);
}
public ParameterCollectionBuilder(Uri uri) : this(uri.OriginalString) { }
public ParameterCollectionBuilder(NameValueCollection baseCollection)
{
foreach (string key in baseCollection.Keys) this[key] = baseCollection[key];
Init(ToString());
}
/// <summary>
///
/// </summary>
/// <param name="baseCollection"></param>
/// <param name="uri"></param>
/// <remarks>Note: any existing params in the uri will override the params in the collection.</remarks>
public ParameterCollectionBuilder(NameValueCollection baseCollection, string uri)
{
foreach (string key in baseCollection.Keys) this[key] = baseCollection[key];
Init(uri);
}
/// <summary>
///
/// </summary>
/// <param name="baseCollection"></param>
/// <param name="uri"></param>
/// <remarks>Note: any existing params in the uri will override the params in the collection.</remarks>
public ParameterCollectionBuilder(NameValueCollection baseCollection, Uri uri) : this(baseCollection, uri.OriginalString) { }
public override string ToString()
{
return Prefix + Query + Suffix;
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <remarks>Overides existing values.</remarks>
public new void Add(string name, object value)
{
Set(name, GetString(value));
}
/// <summary>
/// Add an array of key-value pairs separated by colon char ':'.
/// </summary>
/// <param name="names">Invalid items will be ignored.</param>
public void AddRange(string[] names)
{
rangeFlag = true;
for (int i = 0; i < names.Length; i++)
{
string item = names[i];
item = item.Replace("?", "");
item = item.Replace("&", "");
item = item.Replace("=", ":");
string[] pair = item.Split(':');
if (pair.Length == 2) Set(pair[0], pair[1]);
}
InitUri(FullString);
rangeFlag = false;
}
public void AppendQueryString(string query)
{
Add(BuildCollection(query));
}
public void RemoveRange(string[] keys)
{
rangeFlag = true;
foreach (string key in keys)
{
Remove(key);
}
InitUri(FullString);
rangeFlag = false;
}
bool rangeFlag = false;
public new void Set(string name, object value)
{
base.Set(name, GetString(value));
if (!rangeFlag && Uri != null) InitUri(FullString);
}
public override void Remove(string name)
{
base.Remove(name);
if (!rangeFlag && Uri != null) InitUri(FullString);
}
public override void Clear()
{
base.Clear();
if (Uri != null) InitUri(FullString);
}
#endregion NameValueCollection Implementation
static string ParseQuery(string uri)
{
string query = "";
if (!uri.Contains('=')) return query;
int
start = 0,
end = uri.Length;
if (uri.Contains('?')) start = uri.IndexOf('?');
if (uri.Contains(':')) end = uri.LastIndexOf(':');
query = uri.Substring(start, (start < end ? end : uri.Length) - start);
return query;
}
void Init(string uri)
{
if (Uri == null)
{
InitUri(uri);
}
OriginalQuery = ParseQuery(uri);
int qIndex = string.IsNullOrEmpty(OriginalQuery) ? uri.Length : uri.IndexOf(OriginalQuery);
Prefix = uri.Substring(0, qIndex);
Suffix = uri.Substring(qIndex + OriginalQuery.Length);
Merge(OriginalQuery);
}
void Merge(string query)
{
NameValueCollection col = BuildCollection(query);
foreach (string key in col.Keys)
{
string value = col[key];
if (!string.IsNullOrEmpty(value)) this[key] = value;
}
}
void InitUri(string uri)
{
try
{
Uri = new Uri(uri);
}
catch { }
}
static string GetString(object value)
{
return value is string ? value as string : value.ToString();
}
static NameValueCollection BuildCollection(string query)
{
NameValueCollection collection = new NameValueCollection();
if (string.IsNullOrEmpty(query) || !query.Contains('=')) return new NameValueCollection();
//Prepare string
query = query.ToLower();
if (!query.StartsWith("?"))
{
if (query.Contains('?')) query = query.Substring(query.IndexOf('?'));
}
query = query.Replace("?", "");
foreach (string pair in query.Split('&'))
{
string[] separated = pair.Split('=');
if (separated.Length == 2) collection[separated[0]] = separated[1];
}
return collection;
}
static string BuildQuery(NameValueCollection parameters)
{
string query = "";
Char separator = '?';
bool first = true;
foreach (string key in parameters.Keys)
{
query += string.Format("{0}{1}={2}", separator, key, parameters[key]);
if (first)
{
first = false;
separator = '&';
}
}
return query;
}
#region Properties
public Uri Uri { get; private set; }
public string Prefix { get; private set; }
public string OriginalQuery { get; private set; }
public string Suffix { get; private set; }
public string OriginalString
{
get
{
return Prefix + OriginalQuery + Suffix;
}
}
public string Query
{
get
{
return BuildQuery(this);
}
}
public string FullString
{
get
{
return ToString();
}
}
#endregion Properties
}
}

I would recommend you take a look at this article on CodeProject.
The author has extended the System.UriBuilder class and added a QueryString property that behaves in much the same way as the HttpRequest.QueryString property.
Using this class your example would become:
UrlBuilder ub = new UrlBuilder("http://www.google.com/search");
ub.QueryString.Add("q", "request");
ub.QueryString.Add("sourceid", "ie8");
string uri = ub.ToString(); //http://www.google.com/search?q=request&sourceid=ie8
It doesn't have a fluent interface like Josh's solution but could be easily extended to include one.

With Flurl [disclosure: I'm the author], your example would look like this:
string uri = "http://www.google.com/search"
.SetQueryParams(new { q = "request", sourceid = "ie8" });
The basic URL builder is available via NuGet:
PM> Install-Package Flurl
There's also a new companion lib that extends Flurl with fluent, testable HTTP:
PM> Install-Package Flurl.Http

Related

Query Cosmos DB to get a list of different derived types using the .Net SDK Microsoft.Azure.Cosmos

We have an interface and a base class with multiple derived types.
public interface IEvent
{
[JsonProperty("id")]
public string Id { get; set; }
string Type { get; }
}
public abstract class EventBase: IEvent
{
public string Id { get; set; }
public abstract string Type { get; }
}
public class UserCreated : EventBase
{
public override string Type { get; } = typeof(UserCreated).AssemblyQualifiedName;
}
public class UserUpdated : EventBase
{
public override string Type { get; } = typeof(UserUpdated).AssemblyQualifiedName;
}
We are storing these events of different derived types in the same container in Cosmos DB using v3 of .Net SDK Microsoft.Azure.Cosmos. We then want to read all the events and have them deserialized to the correct type.
public class CosmosDbTests
{
[Fact]
public async Task TestFetchingDerivedTypes()
{
var endpoint = "";
var authKey = "";
var databaseId ="";
var containerId="";
var client = new CosmosClient(endpoint, authKey);
var container = client.GetContainer(databaseId, containerId);
await container.CreateItemAsync(new UserCreated{ Id = Guid.NewGuid().ToString() });
await container.CreateItemAsync(new UserUpdated{ Id = Guid.NewGuid().ToString() });
var queryable = container.GetItemLinqQueryable<IEvent>();
var query = queryable.ToFeedIterator();
var list = new List<IEvent>();
while (query.HasMoreResults)
{
list.AddRange(await query.ReadNextAsync());
}
Assert.NotEmpty(list);
}
}
Doesn't seem to be any option to tell GetItemLinqQueryable how to handle types. Is there any other method or approach to support multiple derived types in one query?
It's ok to put the events in some kind of wrapper entity if that would help, but they aren't allowed to be stored as an serialized sting inside a property.
The comment from Stephen Clearly pointed me in the right direction and with the help of this blog https://thomaslevesque.com/2019/10/15/handling-type-hierarchies-in-cosmos-db-part-2/ I ended up with a solution similar to the following example were we have a custom CosmosSerializer that uses a custom JsonConverter that reads the Type property.
public interface IEvent
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("$type")]
string Type { get; }
}
public abstract class EventBase: IEvent
{
public string Id { get; set; }
public string Type => GetType().AssemblyQualifiedName;
}
public class UserCreated : EventBase
{
}
public class UserUpdated : EventBase
{
}
EventJsonConverter reads the Type property.
public class EventJsonConverter : JsonConverter
{
// This converter handles only deserialization, not serialization.
public override bool CanRead => true;
public override bool CanWrite => false;
public override bool CanConvert(Type objectType)
{
// Only if the target type is the abstract base class
return objectType == typeof(IEvent);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// First, just read the JSON as a JObject
var obj = JObject.Load(reader);
// Then look at the $type property:
var typeName = obj["$type"]?.Value<string>();
return typeName == null ? null : obj.ToObject(Type.GetType(typeName), serializer);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotSupportedException("This converter handles only deserialization, not serialization.");
}
}
The NewtonsoftJsonCosmosSerializer takes a JsonSerializerSettings that it uses for serialization.
public class NewtonsoftJsonCosmosSerializer : CosmosSerializer
{
private static readonly Encoding DefaultEncoding = new UTF8Encoding(false, true);
private readonly JsonSerializer _serializer;
public NewtonsoftJsonCosmosSerializer(JsonSerializerSettings settings)
{
_serializer = JsonSerializer.Create(settings);
}
public override T FromStream<T>(Stream stream)
{
if (typeof(Stream).IsAssignableFrom(typeof(T)))
{
return (T)(object)stream;
}
using var sr = new StreamReader(stream);
using var jsonTextReader = new JsonTextReader(sr);
return _serializer.Deserialize<T>(jsonTextReader);
}
public override Stream ToStream<T>(T input)
{
var streamPayload = new MemoryStream();
using var streamWriter = new StreamWriter(streamPayload, encoding: DefaultEncoding, bufferSize: 1024, leaveOpen: true);
using JsonWriter writer = new JsonTextWriter(streamWriter);
writer.Formatting = _serializer.Formatting;
_serializer.Serialize(writer, input);
writer.Flush();
streamWriter.Flush();
streamPayload.Position = 0;
return streamPayload;
}
}
The CosmosClient is now created with our own NewtonsoftJsonCosmosSerializer and EventJsonConverter.
public class CosmosDbTests
{
[Fact]
public async Task TestFetchingDerivedTypes()
{
var endpoint = "";
var authKey = "";
var databaseId ="";
var containerId="";
var client = new CosmosClient(endpoint, authKey, new CosmosClientOptions
{
Serializer = new NewtonsoftJsonCosmosSerializer(new JsonSerializerSettings
{
Converters = { new EventJsonConverter() }
})
});
var container = client.GetContainer(databaseId, containerId);
await container.CreateItemAsync(new UserCreated{ Id = Guid.NewGuid().ToString() });
await container.CreateItemAsync(new UserUpdated{ Id = Guid.NewGuid().ToString() });
var queryable = container.GetItemLinqQueryable<IEvent>();
var query = queryable.ToFeedIterator();
var list = new List<IEvent>();
while (query.HasMoreResults)
{
list.AddRange(await query.ReadNextAsync());
}
Assert.NotEmpty(list);
}
}

How to insert gridview data into database as a list(Multiple rows) WCF

I have tried to send data as a string and it is working correctly. but now i want to insert all gridview data as a list into database. Code is here
public interface IService1
{
[OperationContract]
string InsertCustomerDetails(UserDetails userInfo);
[OperationContract]
[WebGet]
List<CustomerTable> GetCustomers();
}
public class UserDetails
{
string Name = string.Empty;
string City = string.Empty;
[DataMember]
public string name
{
get { return Name; }
set { Name = value; }
}
[DataMember]
public string city
{
get { return City; }
set { City = value; }
}
public class Service1 : IService1
{
public string InsertCustomerDetails(UserDetails userInfo)
{
using(DataContext db=new DataContext())
{
CustomerTable customer = new CustomerTable();
customer.Name = userInfo.name;
customer.City = userInfo.city;
db.CustomerTables.Add(customer);
db.SaveChanges();
}
return "name= " + userInfo.name + " city= " + userInfo.city;
}
}
}
WEB Form Code
protected void ButtonADD_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView2.Rows.Count; i++) {
UserDetails info = new UserDetails();
info.name = GridView2.Rows[i].Cells[0].Text;
info.city = GridView2.Rows[i].Cells[1].Text;
obj.InsertCustomerDetails(info);
} }
In Iservice1 class use this
public List<CustomerTable> InsertCustomerDetails(UserDetails userInfo)
{
using(DataContext db=new DataContext())
{
CustomerTable customer = new CustomerTable();
customer.Name = userInfo.name;
customer.City = userInfo.city;
db.CustomerTables.Add(customer);
db.SaveChanges();
return db.CustomerTables.ToList();
}
Use this in interface. Make setter getters in class UserDetails
[OperationContract]
List<CustomerTable> InsertCustomerDetails(UserDetails userInfo);
I Have done this. Just facing problem in Web Form. Any Help will be appriciated. I want to send dqata as list into database

ServiceStack RSS serialisation issue

I'm trying to create an RSS feed for a ServiceStack Service. I've followed various examples as closely as I can. My problem is that I get no output and I am not sure how to troubleshoot the issue. I suspect I have done something wrong on the serialisation. Here is (a simplified version of) what I have
My DTO's are
using System.Collections.Generic;
using ServiceStack;
using Library;
[Route("/MyCollection/Tomorrow/{ID}", "GET, POST")]
[Api("MyCollections Delivery")]
public class MyCollectionTomorrow
: IReturn<MyCollectionTomorrowResponse>
{
public long ID { get; set; }
}
public class MyCollectionTomorrowResponse : IHasResponseStatus
{
public long ID { get; set; }
public List<MyCollection> Result { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
public class MyCollection
{
public string Description { get; set; }
public string MyCollectionDayOfWeek { get; set; }
public DateTime MyCollectionDate { get; set; }
public bool Assisted { get; set; }
public string RoundType { get; set; }
public string Description { get; set; }
}
My service is
using System;
using Library;
using ServiceStack;
using ServiceStack.Configuration;
using System;
using Library;
using ServiceStack;
using ServiceStack.Configuration;
using MyCollection.Tomorrow;
using MyCollections.Tomorrow;
public class MyCollectionTomorrowService : Service
{
public object Any(WasteCollectionTomorrow request)
{
int id;
var param = new CollectionTomorrow();
param.ID = ID;
var response = client.Get<CollectionTomorrowResponse>(param);
return response;
}
catch (Exception ex)
{
var response = new CollectionTomorrowResponse();
response.Result = null
var status = new ResponseStatus { Message = ex.Message, StackTrace = ex.StackTrace };
response.ResponseStatus = status;
return response;
}
}
}
and my media type is
namespace DataFeedServices
{
using System;
using System.IO;
using System.ServiceModel.Syndication;
using System.Text;
using System.Xml;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.Web;
using MyCollections.Tomorrow;
public class RssFormat
{
private const string RssContentType = "application/rss+xml";
public static void Register(IAppHost appHost)
{
appHost.ContentTypes.Register(RssContentType, SerializeToStream, DeserializeFromStream);
}
public static void SerializeToStream(IRequest req, object response, Stream stream)
{
StreamWriter sw = null;
try
{
var syndicationFeedResponse = response as MyCollectionResponse;
sw = new StreamWriter(stream);
if (response != null)
{
WriteRssCollectionFeed(sw, syndicationFeedResponse);
}
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
}
public static void WriteRssCollectionFeed(StreamWriter sw, MyCollectionResponse Mycollections)
{
const string Baseuri = "example.com";
try
{
var uri = new Uri(Baseuri);
var syndicationFeed = new SyndicationFeed(
"MyCollection Service",
"Mycollections " ,
uri);
syndicationFeed.Authors.Add(new SyndicationPerson("email#mysite.com"));
if (Mycollections.Result != null)
{
foreach (var cats in Mycollections.Result)
{
syndicationFeed.Categories.Add(new SyndicationCategory(cats.RoundID));
}
}
syndicationFeed.Generator = "MyApp";
syndicationFeed.Copyright = new TextSyndicationContent("Copyright 2015");
syndicationFeed.LastUpdatedTime = DateTime.Now;
if (Mycollections.Result != null)
{
// set items
foreach (var coll in Mycollections.Result)
{
var item = new SyndicationItem { Title = new TextSyndicationContent(coll.CollectionDate) };
item.Links.Add(new SyndicationLink(uri));
item.Authors.Add(new SyndicationPerson("email#mysite.com"));
var itemContent = new StringBuilder();
itemContent.Append("My Item content");
item.Content = new TextSyndicationContent(
itemContent.ToString(),
TextSyndicationContentKind.Plaintext);
}
}
Rss20FeedFormatter rssFeed = syndicationFeed.GetRss20Formatter();
var xwriter = XmlWriter.Create(sw);
rssFeed.WriteTo(xwriter);
}
catch (Exception)
{
throw new Exception("Something bad happened");
}
}
public static object DeserializeFromStream(Type type, Stream stream)
{
throw new NotImplementedException();
}
}
}
Since your ContentType is not reusable and coupled to a specific MyCollectionResponse, it's easier to just return a raw string with the RSS XML:
[AddHeader(ContentType = "application/rss+xml")]
public object Any(WasteCollectionTomorrow request)
{
//..
return rssXml;
}
You can also write it directly to the Response Output Stream with something like:
public object Any(WasteCollectionTomorrow request)
{
//..
base.Response.ContentType = "application/rss+xml";
RssFormat.SerializeToStream(response, Response.OutputStream);
base.Response.EndRequest();
return null;
}

Navigate to views in prism containing danish letters

I have views with ÆØÅ in there names. If I try to navigate to one of them I get system.object instead of my view.
unity.RegisterTypeForNavigation();
public static void RegisterTypeForNavigation<T>(this IUnityContainer container)
{
container.RegisterType(typeof(object), typeof(T), typeof(T).FullName);
}
Should I escape the FullName?
Yes you should url encode the type name, fx. like this
public static class UnityExtensions
{
public static void RegisterTypeForNavigation<T>(this IUnityContainer container)
{
container.RegisterType(typeof(object), typeof(T),Replace(typeof(T).FullName));
}
public static string Replace(string url)
{
return WebUtility.UrlEncode(url);
}
}
And you also have to urlencode when you navigate.
public void Navigate(object navigatePath, string region = RegionNames.ContentRegion,
NavigationParameters parameters = null,
Action<NavigationResult> navigationCallback = null)
{
if (navigatePath == null) return;
var path = UnityExtensions.Replace(navigatePath.ToString());
if (parameters == null)
_regionManager.RequestNavigate(region, path);
else
{
var uri = new Uri(path, UriKind.RelativeOrAbsolute);
if (navigationCallback == null)
_regionManager.RequestNavigate(region, uri, parameters);
else
_regionManager.RequestNavigate(region, uri, navigationCallback, parameters);
}
}
That's it

Data Annotations - using an attribute extension and storing regular expressions in a resource file

I am currently working with MVC4 data annotations to handle validation. I am working on a site that will be very much international and as such I keep all of my text in resource files.
I also want to keep regular expressions for validation in resource files so I can use the same code to check, for example, Post Codes (UK) and Zip Codes (US) just by using a different RegEx (and resources for the different names etc).
I have the below attribute which is already pulling the error message from a resource file. How can I have it get the regex from a resource file too?
[RegularExpression(#"^[\w]{1,2}[0-9]{1,2}[\w]?\s?[0-9]{1,2}[\w]{1,2}$", ErrorMessageResourceType = typeof(Resources.ValidationMessages), ErrorMessageResourceName = "validPostcode")]
EDIT (AGAIN)
Where I am now
Following the answer below and some additional searching around, I have the following:
In Global.asax.cs I have added the below line to ensure client side validation is invoked
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalisedAttribute), typeof(RegularExpressionAttributeAdapter));
In my model, I have this call to the attribute extension
[Localised(typeof(Resources.FormValidation), "postcodeRegEx", "postcodeMsg")]
And finally, the attribute extension for localised regex validation
public class LocalisedAttribute : RegularExpressionAttribute
{
public LocalisedAttribute(Type resource, string regularExpression, string errorMessage)
: base(GetRegex(regularExpression))
{
ErrorMessageResourceType = resource;
ErrorMessageResourceName = errorMessage;
}
private static string GetRegex(string value)
{
return Resources.FormValidation.ResourceManager.GetString(value);
}
}
This works, but ONLY the first time I use it when starting the application.
I am going to open another question to get around that problem - it's not directly related to the original request, doesn't seem to be relevant to most peoples implementation and doesn't seem to be specific to data annotations.
I already have some extended kind of RegularExpressionAttribute implementation, that allows to use resources for regex pattern. It looks like:
public class RegularExpressionExAttribute : RegularExpressionAttribute, IClientValidatable
{
private Regex regex { get; set; }
private string pattern;
private string resourceName;
private Type resourceType;
/// <summary>
/// constructor, calls base with ".*" basic regex
/// </summary>
/// <param name="resName">resource key</param>
/// <param name="resType">resource type</param>
public RegularExpressionExAttribute(string resName, Type resType)
: base(".*")
{
resourceName = resName;
resourceType = resType;
}
/// <summary>
/// override RegularExpressionAttribute property
/// </summary>
public new string Pattern
{
get
{
SetupRegex();
return pattern;
}
}
/// <summary>
/// loads regex from resources
/// </summary>
private void SetupRegex()
{
ResourceAccessor ra = new ResourceAccessor(resourceName, resourceType);
pattern = ra.resourceValue;
regex = new Regex(pattern);
}
/// <summary>
/// override validation with our regex
/// </summary>
/// <param name="value">string for validation</param>
/// <returns></returns>
public override bool IsValid(object value)
{
SetupRegex();
string val = Convert.ToString(value);
if (string.IsNullOrEmpty(val))
return true;
var m = regex.Match(val);
return (m.Success && (m.Index == 0));
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metaData, ControllerContext controllerContext)
{
yield return new ModelClientValidationRegexRule(base.ErrorMessageString, this.Pattern);
}
}
Also it's using ResourceAccessor class to get regex out of resources
public class ResourceAccessor
{
private string resourceName;
private Type resourceType;
private Func<string> accessor;
private string _resourceValue;
public ResourceAccessor(string resourceName, Type resourceType)
{
this.resourceName = resourceName;
this.resourceType = resourceType;
}
public string resourceValue
{
get
{
SetupAccessor();
return accessor();
}
}
private void SetupAccessor()
{
if (accessor != null) //already set
return;
string localValue = _resourceValue;
bool flag1 = !string.IsNullOrEmpty(resourceName);
bool flag2 = !string.IsNullOrEmpty(localValue);
bool flag3 = resourceType != (Type)null;
if (flag1 == flag2)
{
throw new InvalidOperationException("Can't set resource value");
}
if (flag3 != flag1)
{
throw new InvalidOperationException("Resource name and type required");
}
if (flag1)
PropertyLookup();
else
{
accessor = (Func<string>)(() => localValue);
}
}
private void PropertyLookup()
{
if (resourceType == (Type)null || string.IsNullOrEmpty(resourceName))
{
throw new InvalidOperationException("Resource name and type required");
}
PropertyInfo property = resourceType.GetProperty(resourceName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (property != (PropertyInfo)null)
{
MethodInfo getMethod = property.GetGetMethod(true);
if (getMethod == (MethodInfo)null || !getMethod.IsAssembly && !getMethod.IsPublic)
property = (PropertyInfo)null;
}
if (property == (PropertyInfo)null)
{
throw new InvalidOperationException("Resource type doesn't have property");
}
else if (property.PropertyType != typeof(string))
{
throw new InvalidOperationException("Resource type must be string");
}
else
{
accessor = (Func<string>)(() => (string)property.GetValue((object)null, (object[])null));
}
}
}
And here is usage samples:
public class SignUpInput
{
[RegularExpressionEx("EmailValidationRegex", typeof(LocalizedResources), ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "invalidEmail")]
public string Email { get; set; }
}
I think yuo can extend RegularExpressionAttribute
public class PostCodeValidationAttribute : RegularExpressionAttribute
{
public PostCodeValidationAttribute()
: base(Resources.PostCodeValidationExpression)
{
}
}
UPDATE
Put culture info name in session for example accordingly with user choice. And use it in
ResourceManager.GetString(value, CultureInfo.CreateSpecificCulture(userCulture));
At first you can test it with hardcode value. Something like this
ResourceManager.GetString(value, CultureInfo.CreateSpecificCulture("en-GB"));
instead
ResourceManager.GetString(value, CultureInfo.CreateSpecificCulture(currentCulture));
or in base constructor
base(GetRegex(regularExpression, ""en-GB""))

Resources