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());
Related
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 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So when i read a tutorial i met that piece of code, and i didn't understand why does " T id" and why author after then write in class "id = _id"?
class Account<T>
{
public T id { get; set; }
public int Sum { get; set; }
public Account(T _id)
{
id = _id;
}
}
He is just naming the variables differently for ease of reading / understanding. What he does is, declaring a property of type T (generic type) with name id and accepting a value for that in the constructor using the variable _id also of type T. and assigning it to the id proprty. this can also be writter as below but for clarity of reading the above approach may be good.
class Account<T>
{
public T id { get; set; }
public int Sum { get; set; }
public Account(T id)
{
this.id = id;
}
}
So the Constructor is theroeticly (practicly) a Method, it takes Parameters.
So you can new T(5).
The Class ITSELF has a Member which is "id", a variable which holds information AS LONG as the Object of this Class is not disposed.
Parameters of a Method are only Viable inside the Method, so _id is only Aviable in the Constructor.
If u want to Create a new Car with ID 5. The Object has to know that its ID 5.
So you Programmer, Create a Car with ID 5
T lambo = new T(5)
Now the Constructor Method has the _id = 5.
In the Constructor while constructing u Mark on the Car that its which it has been given
id = _id
now id = 5
and _id is gone after the constructor
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?
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);
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
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
I want to add a property to the Node which is optional for the user to set.
How do I add another class level attribute aside from the one which already exists ?
VirtualName - this would give users the ability to name the node whatever they want.
I already implemented the ClassName. I will eventually want to add other class object level attributes like color and icon.
namespace NodeDemo
{
[NodeAttribute("DemoNode")]
public class DemoNode
{
[InputPinAttribute("Radius")]
public int Radius { get; set; }
[InputPinAttribute("Width Segments")]
public int WidthSegs { get; set; }
[InputPinAttribute("Height Segments")]
public int HeightSegs { get; set; }
}
}
Just add as many as you want:
namespace NodeDemo
{
public class ColorAttribute : Attribute
{
public string Color {get;set;}
public ColorAttribute(string color)
{
Color = color;
}
}
[NodeAttribute("DemoNode")]
[ColorAttribute("Red")]
public class DemoNode
{
...
}
}
You can latter check attribute using Type.Attributes property
string color = null;
Type myType = typeof(DemoNode);
ColorAttribute cAttribute = (ColorAttribute)Attribute.GetCustomAttribute(myType, typeof(ColorAttribute));
if (cAttribute != null)
{
color = cAttribute.Color;
}