Get property info of all properties which values currently equal 'default' [duplicate] - c#

This question already has answers here:
Programmatic equivalent of default(Type)
(14 answers)
Closed 8 years ago.
So, I need to retrieve all properties of an instance which currently have a value that matches the default value of their respective type. Something along the lines of
GetType().GetProperties().Where(x =>
x.GetValue(this).Equals(default(x.PropertyType)));
This obviously doesn't work because it seems 'x' cannot be resolved anymore at this point. What could I do?

The problem is slightly different. You cannot pass a runtime instance of Type to default. Your problem can be simplified to this:
var type = typeof (string);
var defaultValue = default(type); // doesn't work
That doesn't work. Instead, you want to get the default value at run time, which has been answered by this question.

Related

Get `Type` for Array of Type at Runtime [duplicate]

This question already has an answer here:
Creating Array Type at runtime
(1 answer)
Closed 9 months ago.
I have a variable prop of type PropertyInfo.
I want to create a Type variable which is an array of the prop.PropertyType.
For normal generic types I can do typeof(myType<>).MakeGenericType(prop.PropertyType) but arrays aren't generics.
The best I've been able to come up with so far is:
Array.CreateInstance(prop.PropertyType, 0).GetType()
But this is really inelligant, am I missing something?
You can use Type.MakeArrayType() to do this:
var arrayType = prop.PropertyType.MakeArrayType();

Copy properties from object A to B when that property is NULL [duplicate]

This question already has answers here:
Merging two objects in C#
(7 answers)
Closed 2 years ago.
I have two objects that are both type Shoppingcart.
One is called shoppingcartA, the other shoppingcartDefault.
shoppingcartA has some of the properties set, but some are NULL.
I want to replace every property of shoppingcartA that is NULL with the value that shoppingcartDefault has.
The problem is that I don't know the names of these properties (or I do, but there are 100 properties and I don't want to manualy type them all).
I've looked at a foreach that loops over every property that shopingcartA has but couldn't find a way to then take that same property from shoppingcartDefault and stick it in there.
You can map like this
foreach (var propertyInfo in test2.GetType().GetProperties())
{
if (propertyInfo.GetValue(test2) == null)
{
propertyInfo.SetValue(test2, propertyInfo.GetValue(test1));
}
}

Is there a way to get all the possible return values of a method in C#? [duplicate]

This question already has an answer here:
Is using static bytecode analysis to determine all the possible paths through a given method a variant of trying to solve the Halting Problem?
(1 answer)
Closed 4 years ago.
Suppose there's a method which returns enum. But it returns only a subset of all the values of the enumeration. Can I find out programmatically which values are obtainable?
Example. I have an enum, which describes color with 100 values. Method GetCurrentTrafficLightsState can return only 3 colors of 100. I want to pass the method GetCurrentTrafficLightsState into some other method and get 3 colors as response.
No there isn't. You cannot even determine if it will return at all, see https://en.wikipedia.org/wiki/Halting_problem

Constructing an enum instance [duplicate]

This question already has answers here:
Can you add to an enum type in run-time
(7 answers)
Closed 8 years ago.
I have this enum function with some elements:
public enum TrackingTypeEnum
{
None,
Start,
PageView,
Foreground,
Background,
Push,
}
And I want to add some elements there, but not manually in the function, i want to use "new" command but dosn't work.
I've tried this:
TrackingTypeEnum Custom = new TrackingTypeEnum;
Any solution?
Thanks!
According to official documentation, you can't use enum keywork in such a way. In sort, an enum is a list of constants you can assign values to.
Read: http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
That's not what enums are for- you should only use them to store every possible state of an object.
If you really do want to use enums, you can store those additional values in a dictionary, like it's covered in this SO answer:
C#: can you add to an enum type in run-time

What's the name of this kind of method, parameters? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can you use optional parameters in C#?
I found in a project where I'm working[language: c#]. A strange signature(at the least for me) like that:
AccessModifier NameOfMethod(sometype param1, bool prmFlagOrSomething = false)
In the msdn library doesn't exist any reference to this kind of method.
That allow avoid pass the parameter prmFlagOrSomething, in this case prmFlagOrSomething have the value false.
That's what happened, but exist documentation?
What's the real name of this kind of method, or parameter?
That's a default parameter. Or as MS calls it, an "Optional Argument":
http://msdn.microsoft.com/en-us/library/dd264739.aspx
It's just an Optional Argument

Categories