How does this code work? Accessing property through another property [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So this is the code:
class Program
{
static void Main(string[] args)
{
Type T = Type.GetType("CSharpLearningPurposes.Program");
PropertyInfo[] properties = T.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine(property.PropertyType.Name);
}
}
}
More specifically, the question is about this line of the code:
Console.WriteLine(propert.PropertyType.Name);
You see here that I access property.PropertyType okay I understand that I am accessing the object's member but I don't understand this: property.PropertyType.Name
What's that doing exactly? Can anybody explain me?

Suppose the following classes:
public class A {
public B Prop {get;set;}
}
public class B {
public string Name {get;set;}
}
If you had an instance of A setup like so:
var example = new A { Prop = new B { Name = "The Name" } };
Then example.Prop.Name would return "The Name" by way of having returned the instance of B assigned to Prop.
You could break this down into a longer form to get an example of how the access works and see how chaining the calls is helpful from a productivity standpoint.
B propVal = example.Prop;
string nameVal = propVal.Name;
Console.WriteLine(nameVal == example.Prop.Name); // True

Related

How can I check for the data type of a dynamic property upon setting a value to it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Say I have this class:
public class Model {
public Type DataType { get; set; }
public dynamic DefaultValue { get; set; }
}
I would like to ensure that the data type of DefaultValue matches the assigned type to DataType as soon as a value gets assigned to the property. What would be a good way to do this?
Please use Generics for this:
public class Model<T>
{
public T DefaultValue { get; set; }
}
You could modify the DefaultValue to
private dynamic _defaultValue;
public dynamic DefaultValue
{
get => _defaultValue;
set
{
if (DataType == null) throw new Exception("Set type first");
if (value.GetType() != DataType) throw new Exception("Wrong type");
_defaultValue = value;
}
}
Although I imagine this could be better implemented with generics. Also what would happen if value is derived from DataType?

Converting dynamic to json (anonymous) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a dynamic object that I have to convert to json (anonymous) for passing to an API as param. What is the best way to do this conversion?
There is this post:
How to convert a dynamic object to JSON string c#?
But this does not quite apply to me as I cannot use var as explained in the comment below.
Thanks
Anand
Trying to covert
dynamic d = new ExpandoObject()
d.prop = "value"
To:
var json = new {prop = "value"}
If you are in .NET Core You can use System.Text.Json;and serialize as dynamic
public static void Main()
{
dynamic w = new ExpandoObject() { Date = DateTime.Now, Item1 = 30 };
w.Item2 = 123;
Console.WriteLine(JsonSerializer.Serialize<dynamic>(w));
}
class ExpandoObject
{
public DateTimeOffset Date { get; set; }
public int Item1 { get; set; }
public int Item2 { get; set; }
}
fiddle here
But I do not understand why do you need to use dynamic (probably there is some logic behind it)
if you are in .Net Framework you need to use Newtonsoft Json which is basically the same stuff
dynamic results = JsonConvert.SerializeObject<dynamic>(w);

Can We Have Class Name Instead Of Return Type In c#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can We Have Class Name Instead Of Return Type ? If Yes Than How And If No Than How ? , Please Explain Me With This Basic Singleton Pattern
class Program
{
static void Main(string[] args)
{
Sample s1;
s1 = Sample.cr();
Sample s2 = Sample.cr();
Console.ReadLine();
}
}
class Sample
{
static Sample p;
private Sample()
{
}
// Why Here Retrun Type Is Missing I Mean Void / Int ?
public static Sample cr()
{
if(p==null)
{
p = new Sample();
}
return p;
}
}
Return type is not missing in your sample. Your cr method has return type, and it is Sample.
Any of your custom class is type too, not only primitive types such as int, string and so on.

Making a list in the property [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a list like that.
bread.BreadAdd(new Bread("DF6", "8", 16, 500));
bread.BreadAdd(new Bread("ZER6D23", "5", 8, 1000));
As I understood, I need to create a code in the bread class, but furthermore I have no idea is has to be in the property? If so, where exactly?
edit: uups. I wrote it wrong. it has to be in a function.
Can be something like this:
using SCG = System.Collections.Generic;
public class Bread {
public class Bread(string name, string version, int foodValue, int weight) {
// ...
}
}
public class Breads {
private readonly SCG.List breads = new SCG.List();
public void AddBread(Bread bread) {
breads.Add(bread);
}
public SCG.IEnumerable Breads {
get { return breads; }
}
}
See below.. You can create a method to add it to a private List variable and then expose it via property or you can have a setter for BreadList property and call breadClass.BreadList.Add((new Bread("DF6", "8", 16, 500));. Either option should be fine.
public class BreadClass
{
private List<Bread> lstbread;
public List<Bread> BreadList
{
get
{
return bread;
}
}
public void BreadAdd(Bread bread)
{
lstbread.Add(bread)
}
}

How to create a new object using reflection in c#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I would like to create a new object that is the instance of the following class.
How to make the object created by relfection equals the instance of the object represented by the class below with reflection c #?
public class cPerson
{
public String name { set; get; }
public String adress { set; get; }
public String phone { set; get; }
}
Maybe you're looking for something like Activator:
var person = Activator.CreateInstance(typeof(cPerson));
Of course, you'll probably be using it when the type is unknown at design time, to create object of the same type...
var newInstance = Activator.CreateInstance(p.GetType());

Categories