what kind of property names can i have ? [duplicate] - c#

This question already has answers here:
.NET valid property names
(4 answers)
Closed 8 years ago.
can we have property names like $ref #schema ? If so, how ? If not why not ?
I am actually to do something like this
dynamic d = new ExpandoObject();
d.$ref = somevlaue;
but I can't do that?
Also , how do I workaround the fact that keywords can't be property names?

Use
public string #class {get; set;}
for using keywords as variable names. For more information you can see the rules for Identifiers.

Since you are mainly interested in the ExpandoObject, a workaround would be to directly set and/or retrieve those property names by casting the ExpandoObject to an IDictionary<string, Object>:
dynamic d = new ExpandoObject();
var dict = (IDictionary<string, Object>)d;
dict["$ref"] = "haha!";

The language has the guideline of acceptable chars of name. In your case you can try to use #ref.

Related

c# Anonymous type. How to use datatype name as member name [duplicate]

This question already has an answer here:
How do I use a C# keyword as a property name?
(1 answer)
Closed 2 months ago.
I have to create a json query dynamically where one of the properties is called "bool". I need this name because the system I send the requests to, expects this naming.
To create the json I use C# anonymous types like:
var myquery = new { bool = "Yes" };
but I'm not allowed to use bool as member name. Is there a fix for that somehow?
I have searched for a solution, without any success. I hope there is an easy fix.
Yes, you put the # character in front of the variable.
var myquery = new { #bool = "Yes" };
You can prefix it with an #.
var myquery = new { #bool = "Yes" };

Convert a Tuple of Uniform Type to a List of that Type [duplicate]

This question already has an answer here:
How to dynamically iterate/detect member types in ValueTuple that has null members? [duplicate]
(1 answer)
Closed 2 years ago.
Let's say I have a tuple that looks like this:
(string, string, string) myTuple = ("Poop","Stack","Overflow");
Is there an elegant way to convert this to a list of strings that would look like this?
var myList = new List<string>() { myTuple.Item1, myTuple.Item2, myTuple.Item3 };
I can't ForEach over a tuple, or convert it into an enumerable it seems...
Thank you!
Assuming that you want to handle value tuples which have fields of the same type, for starters you can use simple reflection:
var myList = myTuple.GetType()
.GetFields()
.Select(fi => fi.GetValue(myTuple))
.Cast<string>()
.ToList();
Which will have some performance penalties.
If you have tuples of the same size you can just introduce the method:
public static List<T> GetFromTuple<T>(ValueTuple<T,T,T> tuple)
{
return new List<T> {tuple.Item1, tuple.Item2, tuple.Item3};
}
Also using reflection you can write a small piece of code which will generate methods for all ValueTuple's of different types (or create T4 template for it).

sort list using property variable [duplicate]

This question already has answers here:
How can I do an OrderBy with a dynamic string parameter?
(11 answers)
Closed 7 years ago.
Suppose I have this list of process or any other object
List<Process> listProcess = new List<Process>();
I can sort it using this line listProcess.OrderBy(p => p.Id);
But what if I have only string name of property obtained in runtime. I assume, I should use reflection to get the property object. Can I use orderby method or I should use Sort and then pass own comparer?
You can have a look at the post referred in the comment. Or, you can achieve that using simple reflection like this
var sortedList = list.OrderBy(o => o.GetType().GetProperty(propName).GetValue(o));
Where
List<object> list; //a list of any object(s)
string propName; //name of the property to be used in OrderBy

Create reference to a class from a string variable [duplicate]

This question already has answers here:
C# Reflection: How to get class reference from string?
(6 answers)
Closed 7 years ago.
Here is what I need to do;
string className = "Customer";
List<className> myList = new List<className>();
className can be any of my Entity Framework classes. I have used Customer just as an example.
I hope this is possible. If so, how?
UPDATE:
I also need to use this approach for retrieving data from an Entity Framework dbContext. For example...
string className = "Customer";
var myData = db.Set<className>();
Sorry. Not sure if I should have created another question here or updated this one. Be gentle with me. I'm new here. :o)
If you want you can use reflection in this case, but I would think over another solution.
Type type = Type.GetType("Namespace.ClassName");
Type listType = typeof(List<>).MakeGenericType(new [] { type } );
IList list = (IList)Activator.CreateInstance(listType);
You can use:
Type customerType = Type.GetType("this.is.a.namespace.Customer");
Related: other question

Convert query string dictionary (or associative array) to dictionary [duplicate]

This question already has answers here:
How to parse a query string into a NameValueCollection in .NET
(19 answers)
Closed 5 years ago.
I'm trying to accept a query string of the form
?param[key1]=value1&param[key2]=value2
and convert it into a Dictionary in C# MVC 4. This is trivial to do in PHP, but I haven't been able to find any method of reproducing this in C#.
I know I can take an array via
?param=value1&param=value2
but that leaves issues of ordering and isn't nearly as useful for my purposes.
In the hopes that this isn't a limitation of the framework, how might I implement a dictionary-style conversion in C#?
To clarify: I'm not looking to convert a query string into an NVC, but rather to convert query string parameters into their own NVCs. This is NOT as simple as ParseQueryString() or the Request.QueryString object.
Can't you just use the Request.QueryString collection that Asp.Net provides for you? Since you are using MVC, it will be on your controllers ControllerContext property (located on that object's HttpContext property).
here I fixed some code that will work for you, defo not the best solutioin but it should work.
string basestring = "?param[key1]=value1&param[key2]=value2";
string newstring = basestring.Replace("?", "").Replace("param[", "").Replace("]", "");
var array = newstring.Split('&');
var dictionary = new Dictionary<string, string>();
foreach (var onestring in array)
{
var splitedstr = onestring.Split('=');
dictionary.Add(splitedstr[0],splitedstr[1]);
}
You can also try something like this
public ActionResult SubmitFormWithFormCollection(FormCollection parameters)
{
foreach (string param in parameters)
{
ViewData[param] = parameters[param];
}
return View();
}

Categories