For an application I'm working on I'm trying to display a template that will show what the parameters for a (runtime-determined) method look like. The test case I'm working on should show "PERSON = (FIRST = first; LAST = last);", where the parameter named Person has type Name, and Name has two properties, First and Last. The following code instead shows "PERSON = ();".
GetNestedTypes is not returning anything, any ideas why?
public static string GetParameterTemplate(MethodInfo method)
{
StringBuilder output = new StringBuilder();
foreach (ParameterInfo pi in method.GetParameters())
{
output.Append(parameterTemplateHelper(pi.Name, pi.ParameterType));
}
return output.ToString();
}
private static string parameterTemplateHelper(string pName, Type pType)
{
string key = pName.ToUpper();
string value = "";
if (pType.IsPrimitive)
{
// it's a primitive
value = pName.ToLower();
}
else if (pType.IsArray)
{
if (pType.GetElementType().IsPrimitive)
{
// array of primitives
value = String.Format("{0}1, {0}2;", pName.ToLower());
}
else
{
// array of objects
StringBuilder sb = new StringBuilder();
foreach (Type t in pType.GetElementType().GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic))
{
sb.Append(parameterTemplateHelper(t.Name, t));
}
value = String.Format("({0}), ({0});", sb);
}
}
else
{
// object
StringBuilder sb = new StringBuilder();
Type[] junk = pType.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic);
foreach (Type t in pType.GetNestedTypes())
{
sb.Append(parameterTemplateHelper(t.Name, t));
}
value = String.Format("({0});", sb.ToString());
}
string output = key + " = " + value.ToString();
return output;
}
Your code is looking for nested types - that is, other types declared within Person. That's not at all the same as looking for properties within Person.
Here's a class with nested types:
public class Name
{
public class Nested1 {}
public class Nested2 {}
}
Here's a class with properties:
public class Name
{
public string Name { get; set; }
public string Name { get; set; }
}
My guess is that your situation is much more like the second one than the first... so use Type.GetProperties instead of Type.GetNestedTypes.
Related
I am new to c# and I am trying to print a user defined structure. Code:
var tracksArr = new[]
{new
{ vessel = GetVesselInfo(aisRecord.VesselId),
points = new[]
{ new
{ stampUtc = aisRecord.Time.ToString("yyyy-MM-ddTHH:mm:ssZ"),
}}}}
foreach (var item in tracksArr)
{Console.WriteLine("qqq: " + item.ToString());}
which prints:
qqq: { vessel = { key = 123456,0, mmsi = 7891011, imo = 0 }, points =
<>f__AnonymousType18`6[System.String,System.Double,System.Double...
what is this mysterious <>f__AnonymousType18 and how do I get the value of points?
For each anonymous type with unique set of fields (the new { ... } statements in means creation of instance of anonymous type) compiler will generate a class, which name will look like <>f__AnonymousType18. This class has overridden ToString method, but arrays/collections - don't and point is an array, so by default ToString returns type name which is YourAnonymousTypeName[] for arrays. You can use string.Join to output your collection:
Console.WriteLine($"qqq: {{vessel = {item.vessel}, points = {string.Join(", ", item.points.Select(p => p.ToString()))}}}");
Or create/use another collection type for points which will have overridden ToString method which returns string with all elements:
public static class ext
{
public static MyList<T> ToMyList<T>(this T[] arr)
{
return new MyList<T>(arr);
}
}
public class MyList<T> : List<T>
{
public MyList(T[] arr)
{
AddRange(arr);
}
public override string ToString()
{
return string.Join(",", this);
}
}
var tracksArr = new[]
{new
{ vessel = 1,
points = new[]
{ new
{ stampUtc = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ"), }
}
.ToMyList()
}
}; // prints "qqq: { vessel = 1, points = { stampUtc = 2020-06-24T14:58:08Z } }"
Following my Is it possible to have a Function that takes any number of variables of any type?
I have the function that gets any number of any type of variables and it works perfectly
public string funcVars(params object[] paths)
{
string strVars = String.Join(", ", paths.Select(x => x.ToString()));
return strVars;
}
To call it I'd simply need to
string someString ="asd"; int someInt = 123; bool someBool=false;
funcVars(someString,someInt,someBool);
And the output would be
asd,123,false
is there any simple way I can also get the variable names as well as their values, so the output would be
asd,123,false,someString,someInt,someBool //(or any other similar form)
Or do I need to hardcode the names every time I call my method ?
funcVars("someString","someInt","someBool",someString,someInt,someBool);
What you really should be doing is creating a class to hold your variables:
internal class MyValues
{
internal string SomeString { get; set; }
internal int SomeInt { get; set; }
internal bool SomeBool { get; set; }
}
Then you can pass an instance of your class:
var mv = new MyValues() { SomeString = "asd", SomeInt = 123, SomeBool = false };
funcVars(mv);
Here is funcVars:
public string funcVars(MyValues values)
{
string strVars =
String.Join(", ", new[] { values.SomeString,
values.SomeInt.ToString(), values.SomeBool.ToString() });
return strVars;
}
Straight up stealing roy.ap's code and adding the "nameof()" method since getting the name of the property seemed to be apart of the question.
class Program
{
internal class MyValues
{
internal string SomeString { get; set; }
internal int SomeInt { get; set; }
internal bool SomeBool { get; set; }
}
static void Main(string[] args)
{
var mv = new MyValues() { SomeString = "asd", SomeInt = 123, SomeBool = false };
Console.WriteLine(funcVars(mv));
Console.ReadLine();
}
public static string funcVars(MyValues values)
{
string strVars =
String.Join(", ", new[]
{
nameof(values.SomeString), values.SomeString,
nameof(values.SomeInt), values.SomeInt.ToString(),
nameof(values.SomeBool), values.SomeBool.ToString()
});
return strVars;
}
}
There really isn't a way to get the variable names via the the function itself because the scope changes once you're in the method. That is even if you pass an array of objects, if you perform a foreach to go through each object you will give the individual objects a new scope specific name.
No, because the variables are not actually passed
No it is not possible, because the variables themselves are not actually passed. Their values are passed.
Consider this code:
string someString ="asd"; int someInt = 123; bool someBool=false;
funcVars(someString,someInt,someBool);
In your call to funcVars, all the parameters are passed by value. All three variables are copied, and copy of them is put on the stack. These stack variables are identified by completely different symbols-- (e.g. paths[0],paths[1], etc.)
After all, what would happen if you called it like this?
funcVars("Hello",245+25,test != null);
Obviously those values do not have variable names. There is no way your function can possibly retrieve what doesn't exist.
Use ExpandoObject instead
The System.Dynamic.ExpandoObject seems like a really good fit for this problem.
var args = new System.Dynamic.ExpandoObject();
args.SomeString = "hello";
args.SomeInt = 32;
args.SomeBool = false;
funcVars(args);
public static string funcVars(ExpandoObject inputs)
{
var sb = new StringBuilder();
foreach (KeyValuePair<string, object> kvp in inputs)
{
sb.Append(String.Format("{0} = {1}", kvp.Key, kvp.Value);
}
return sb.ToString();
}
I have this class:
public class allFields
{
public string EAN { get; set; }
public string title { get; set; }
public string qty { get; set; }
public string price { get; set; }
public DateTime date { get; set; }
}
And a function that return an anonymous type:
public IEnumerable<object> stockEtatQty()
{
List<allFields> afList = new List<allFields>();
var query = from x in ctx.book
where x.qty > 0
select x;
foreach (var item in query)
{
allFields af = new allFields();
af.EAN = item.EAN;
af.title = item.Titre;
af.qty = ""+item.Quantite;
afList.Add(af);
}
var q = from x in afList
select new { EAN=x.EAN, Title=x.title, Quantity=x.qty };
return q; //q is a IEnumerable<'a> where a is new {string EAN, string Title, string Quantity}
}
In my WinForm a use this function as below:
private void QuantityToolStripMenuItem_Click(object sender, EventArgs e)
{
ServiceStock sstock = new ServiceStock();
var q = sstock.stockEtatQty().ToList();// q is a list<object>
string str = "";
foreach (var item in q)
{
str += item + Environment.NewLine;
}
MessageBox.Show(str);
}
The result is:
{ EAN = 1, Title = CSharp Security, Quantity = 970 }
{ EAN = 2, Title = MISC, Quantity = 100 }
...
What I want?
I want not like the result above, but separate each field apart of the item in the loop foreach, e.g get item.EAN, item.Title and item.Quantity.
If there is no solution for my problem I would like to know an alternative,
Thanks for help.
The obvious solution is to create a custom type (let's call it BookInfo) and return a IEnumerable<BookInfo> instead of a IEnumerable<object> (and maybe override ToString if you want to put the formatting into this class itself).
Then you can easily format the output.
public class BookInfo
{
public string EAN {get;set;}
public string Title {get;set;}
public int Quantity {get;set;}
}
public IEnumerable<BookInfo> stockEtatQty()
{
...
var q = from x in afList
select new BookInfo { EAN=x.EAN, Title=x.title, Quantity=x.qty };
return q;
}
private void QuantityToolStripMenuItem_Click(object sender, EventArgs e)
{
ServiceStock sstock = new ServiceStock();
var q = sstock.stockEtatQty();
var message = string.Join(Environment.NewLine,
q.Select(item => String.Format("{0} {1} {2}", item.EAN, item.Title, item.Quantity)));
MessageBox.Show(message);
}
Since the static type information about the object of anonymous type is lost by the time that you exit stockEtatQty() method, you could cast the object to dynamic and access fields like this:
str = string.Join(Environment.NewLine, q.Cast<dynamic>().Select(item =>
string.Format("{0} {1} {2}", item.EAN, item.Title, item.Quantity)
));
The cast to dynamic tells the compiler that EAN, Title, and Quantity need to be resolved at runtime.
Note that I also replaced the foreach loop with a call to string.Join to improve performance: repeated string concatenation creates unnecessary partial string objects, which string.Join avoids. Another solution would be to use StringBuider instead of string concatenation +=.
stockEtatQty is in a project (Service) and QuantityToolStripMenuItem_Click is in another project (View)
Unfortunately, this means that you would not be able to use anonymous types: anonymous types are generated with internal visibility, limiting their use to the assembly in which they are produced. You can use a work-around based on ExpandoObject described in this answer:
var q = afList.Select(x => {
dynamic res = new ExpandoObject();
res.EAN=x.EAN;
res.Title=x.title;
res.Quantity=x.qty;
return res;
});
Create a new class that represents the new object structure and return that.
var q = from x in afList
select new SmallerType { EAN=x.EAN, Title=x.title, Quantity=x.qty };
WinForm Function
foreach (SmallerType item in q)
{
//
}
You can use collection of dynamic objects instead of simple objects as return type of your method:
public IEnumerable<dynamic> stockEtatQty()
Then you will not have IntelliSense but at runtime properties will be found:
foreach (var item in sstock.stockEtatQty())
str += String.Format("{0}", item.EAN) + Environment.NewLine;
But I suggest you to create custom class with EAN, Title and Quantity properties. Or just use your allFields instead of anonymous objects.
Consider also to use StringBuilder for string creation to avoid creating lot of in-memory strings:
var builder = new StringBuilder();
foreach (var item in sstock.stockEtatQty())
builder.AppendFormat("{0}{1}", item.EAN, Environment.NewLine);
MessageBox.Show(builder.ToString());
Sorry if the title does not reflect what I actually want.
I'm creating a generic class for selecting, updating, inserting and deleting dates from and to a database.
Basically, I want a function that gives me back an ObservableCollection<"can be anything"> ==> Where anything is a class and not strings. I would like to know if it is possible to do this, if yes, please,help me how I can achieve this.
this is my starting point:
//class a
public static ObservableCollection<ContactPerson> contactPersons = new ObservableCollection<ContactPerson>();
public static ObservableCollection<ContactPerson> getContactPerson()
{
contactPersons = (ObservableCollection<ContactPerson>)DBConnection.GetDataOutDatabase(typeof(ContactPerson), "Contactpersoon");
return contactPersons;
}
//class b
public static Object GetDataOutDatabase(Type myType,String table)
{
ObservableCollection<Object> objecten = new ObservableCollection<Object>();
string sql = "SELECT * FROM " + table;
DbDataReader reader = Database.GetData(sql);
while (reader.Read())
{
objecten.Add(Create(myType, reader));
}
return objecten;
}
private static Object Create(Type myType, IDataRecord record)
{
PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
for (int i = 0; i < myPropertyInfo.Length; i++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
String name = myPropInfo.Name;
Type type = myPropInfo.PropertyType;
}
return null;
}
And this is what I ultimately want to get. Is this possible?
//ContactPerson cp = new ContactPerson();
//cp.ID = (record["ID"].ToString());
//cp.Name = record["Name"].ToString();
//cp.Company = record["Company"].ToString();
//cp.JobTitle = new ContactPersonTitle()
//{
// Name = record["JobTitle"].ToString(),
//};
//cp.JobRole = new ContactPersonType()
//{
// Name = record["JobRole"].ToString(),
//};
//cp.City = record["City"].ToString();
//cp.Email = record["Email"].ToString();
//cp.Phone = record["Phone"].ToString();
//cp.Cellphone = record["Cellphone"].ToString();
Many thanks!
You can actually do this with reflection in generic methods.
public class DBConnection
{
public static ObservableCollection<T> GetDataOutDatabase<T>(string table)
{
var objecten = new ObservableCollection<T>();
string sql = "SELECT * FROM " + table;
DbDataReader reader = Database.GetData(sql);
while (reader.Read())
{
objecten.Add(Create<T>(reader));
}
return objecten;
}
public static T Create<T>(IDataRecord record)
{
var properties = typeof(T).GetProperties();
var returnVal = Activator.CreateInstance(typeof(T));
properties.ToList().ForEach(item =>
{
try
{
if (item.PropertyType.IsPrimitive)
{
item.SetValue(returnVal, Convert.ChangeType(record[item.Name].ToString(), item.PropertyType),null);
}
else
{
object[] parameters = {record};
var value =
typeof(DBConnection).GetMethod("Create").MakeGenericMethod(item.PropertyType).Invoke(null, parameters);
item.SetValue(returnVal,value,null);
}
}
catch
{
Write("Property Not Found");
}
});
return (T)returnVal;
}
}
The example above does assume that all properties names match the column names you are retrieving from your database communication. For instance in the ContactPersonTitle above rather than Name you would need to have JobTitle as the property name.
Not as you are currently doing it. You should look into the entity framework which allows translation of database tables datacollections.
have a look at:
http://www.codeproject.com/Articles/363040/An-Introduction-to-Entity-Framework-for-Absolute-B
this is my first question in stackoverflow and I am a beginner in using reflection.
I would like to dump all values of an object instance for reference (to keep track about used values on a test). I am using Compact Framework 3.5 not the full framework. Keep that in mind for your suggestions.
Imagine following classes:
public class Camera : IDisposable
{
public Camera.FilenameProperties SnapshotFile;
public double DigitalZoomFactor { get; set; }
public bool DisplayHistogram { get; set; }
public int ImageUpdateInterval { get; set; }
public Camera.ImprintCaptionPosType ImprintCaptionPos { get; set; }
public string ImprintCaptionString { get; set; }
}
where the 'special' types are:
public class FilenameProperties
{
public string Directory { get; set; }
public string Filename { get; set; }
public Camera.FilenamePaddingType FilenamePadding { get; set; }
public Camera.ImageType ImageFormatType { get; set; }
public Camera.ImageResolutionType ImageResolution { get; set; }
public int JPGQuality { get; set; }
public void Restore();
public void Save();
public enum Fnametype
{
tSnapshot = 0,
tCircularCapture = 1,
}
}
public enum ImprintCaptionPosType
{
Disabled = 0,
LowerRight = 1,
LowerLeft = 2,
LowerCenter = 3,
UpperRight = 4,
UpperLeft = 5,
UpperCenter = 6,
Center = 7,
}
Now, I can get the 'base' names and properties and the field names of an instance of camera:
Camera cam = new Camera();
dumpProperties(cam);
...
void dumpProperties(object oClass)
{
System.Diagnostics.Debug.WriteLine(oClass.ToString());
FieldInfo[] _Info = oClass.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
for(int i = 0; i<_Info.Length; i++)
{
System.Diagnostics.Debug.WriteLine(_Info[i].Name + ":'" + _Info[i].GetValue(oClass).ToString()+"'");
}
foreach (PropertyInfo pi in oClass.GetType().GetProperties())
{
System.Diagnostics.Debug.WriteLine(pi.Name + ":'" + pi.GetValue(oClass, null)
+ "' Type=" + pi.PropertyType.ToString());
}
}
and then get soemthing like this:
Intermec.Multimedia.Camera
SnapshotFile:'Intermec.Multimedia.Camera+FilenameProperties'
DigitalZoomFactor:'1' Type=System.Double
DisplayHistogram:'False' Type=System.Boolean
ImageUpdateInterval:'1' Type=System.Int32
ImprintCaptionPos:'Disabled' Type=Intermec.Multimedia.Camera+ImprintCaptionPosType
ImprintCaptionString:'' Type=System.String
Now, for simple properties like DigitalZoomFactor and ImageUpdateInterval I get what I need, but for the nested class (correct wording?) I only get the type as for example with SnapshotFile. For the nested enum I get the value as with 'ImprintCaptionPos'.
How can I get the values of the nested values like FilenameProperties.Filename of the SnapshotFile field/property?
If I use dumpProperties(cam.SnapshotFile), I get the output I am looking for:
Intermec.Multimedia.Camera+FilenameProperties
Directory:'\Program Files\FrmCamera' Type=System.String
Filename:'myphoto' Type=System.String
ImageFormatType:'JPG' Type=Intermec.Multimedia.Camera+ImageType
FilenamePadding:'None' Type=Intermec.Multimedia.Camera+FilenamePaddingType
ImageResolution:'Medium' Type=Intermec.Multimedia.Camera+ImageResolutionType
JPGQuality:'100' Type=System.Int32
But how can I automate that?
I did a lot of search and test coding but was unable to find a solution. The problem seems to be getting the instance of the field to be able to iterate thru it.
I do not have the source code of the Camera class, so I cannot add or remove code in there.
Can anyone help?
I need to get something like the debugger shows:
You simply need to use recursion, and loop back into the method if your property is a class. Here's an example of an XML Serialization routine we use, that effectively walks the properties of a target using reflection, and generates an XElement from it. Your logic would be somewhat different as you're not going to build up XML, but the structure of what you're going to do will be pretty similar.
public XElement Serialize(object source,
string objectName,
bool includeNonPublicProperties)
{
XElement element;
var flags = BindingFlags.Instance | BindingFlags.Public;
if(includeNonPublicProperties)
{
flags |= BindingFlags.NonPublic;
}
var props = source.GetType().GetProperties(flags);
var type = source.GetType();
string nodeName;
if(objectName == null)
{
if (type.IsGenericType)
{
nodeName = type.Name.CropAtLast('`');
}
else
{
nodeName = type.Name;
}
}
else
{
nodeName = objectName;
}
element = new XElement(nodeName);
foreach (var prop in props)
{
string name = prop.Name;
string value = null;
bool valIsElement = false;
if (!prop.CanRead) continue;
if(prop.PropertyType.IsEnum)
{
value = prop.GetValue(source, null).ToString();
}
else
{
string typeName;
if (prop.PropertyType.IsNullable())
{
typeName = prop.PropertyType.GetGenericArguments()[0].Name;
}
else
{
typeName = prop.PropertyType.Name;
}
switch (typeName)
{
case "String":
case "Boolean":
case "Byte":
case "TimeSpan":
case "Single":
case "Double":
case "Int16":
case "UInt16":
case "Int32":
case "UInt32":
case "Int64":
case "UInt64":
value = (prop.GetValue(source, null) ?? string.Empty).ToString();
break;
case "DateTime":
try
{
var tempDT = Convert.ToDateTime(prop.GetValue(source, null));
if (tempDT == DateTime.MinValue) continue;
value = tempDT.ToString("MM/dd/yyyy HH:mm:ss.fffffff");
}
catch(Exception ex)
{
continue;
}
break;
default:
var o = prop.GetValue(source, null);
XElement child;
if (o == null)
{
child = new XElement(prop.Name);
}
else
{
child = Serialize(o, prop.Name, includeNonPublicProperties);
}
element.Add(child);
valIsElement = true;
break;
}
}
if (!valIsElement)
{
element.AddAttribute(name, value);
}
}
return element;
}
OK, I find a way (a workaround) to get all properties (in an XML but who cares) using the code from here:
The output is xml like but acceptable for me. Here an excerpt:
<xml version="1.0" encoding="utf-8">
<Camera xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
<ImprintCaptionPos>Disabled</ImprintCaptionPos>
<SnapshotFile>
<Directory>\\Program Files\\FrmCamera</Directory>
<Filename>myphoto</Filename>
<ImageFormatType>JPG</ImageFormatType>
<FilenamePadding>None</FilenamePadding>
<ImageResolution>Medium</ImageResolution>
<JPGQuality>100</JPGQuality>
</SnapshotFile>
...
In my code I just have to call
string s = serialization.mySerialize.SerializeObject<Intermec.Multimedia.Camera>(cam);
To get a 'dump' of all current properties of the instance.
Thanks to all for your help. Possibly I was misunderstood with my question and reflection is unable to give what I want.
Thanks
Josef
You have to do the iteration to all inner object members like you did for outer class. Will be an exercise for you to implement it for complete set of .NET types. Below is the pseudo code of simple implementation
void dumpProperties(object target)
{
if (target.GetType().IsSimple() || target.GetType().IsMethodImplemented("ToString"))
System.Diagnostics.Debug.WriteLine(target.ToString());
else if (target is IEnumerable)
{
foreach (object item in (IEnumerable)target)
{
System.Diagnostics.Debug.WriteLine(dumpProperties(target));
}
}
else
{
foreach (FieldInfo fieldInfo in target.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
{
System.Diagnostics.Debug.WriteLine(dumpProperties(fieldInfo.FieldHandle.Value));
}
foreach (PropertyInfo propertyInfo in oClass.GetType().GetProperties())
{
System.Diagnostics.Debug.WriteLine(dumpProperties(propertyInfo.GetGetMethod().MethodHandle.Value));
}
}
}
Or you can use Cinchoo framework, which has built in function to dump any object.
Console.WriteLine(ChoObject.ToString(camera));