c# gettype of object from class - c#

How could I make this work?:
public class myClass
{
public string first;
public int second;
public string third;
}
public string tester(object param)
{
//Catch the name of what was passed not the value and return it
}
//So:
myClass mC = new myClass();
mC.first = "ok";
mC.second = 12;
mC.third = "ko";
//then would return its type from definition :
tester(mC.first) // would return : "mc.first" or "myClass.first" or "first"
//and
tester(mC.second) // would return : "mc.second" or "myClass.second" or "second"

In the absence of infoof, the best you can do is Tester(() => mC.first) via expression trees...
using System;
using System.Linq.Expressions;
public static class Test
{
static void Main()
{
//So:
myClass mC = new myClass();
mC.first = "ok";
mC.second = 12;
mC.third = "ko";
//then would return its type from definition :
Tester(() => mC.first); // writes "mC.first = ok"
//and
Tester(() => mC.second); // writes "mC.second = 12"
}
static string GetName(Expression expr)
{
if (expr.NodeType == ExpressionType.MemberAccess)
{
var me = (MemberExpression)expr;
string name = me.Member.Name, subExpr = GetName(me.Expression);
return string.IsNullOrEmpty(subExpr)
? name : (subExpr + "." + name);
}
return "";
}
public static void Tester<TValue>(
Expression<Func<TValue>> selector)
{
TValue value = selector.Compile()();
string name = GetName(selector.Body);
Console.WriteLine(name + " = " + value);
}
}

This is not possible. Variable names don't exist in compiled code, so there's no way you can retrieve a variable name at runtime

That's not possible. "param" will have no information on where the value came from.
When calling tester(), a copy of the value in one of the properties is made, so the "link" to the property is lost.

Related

To seed type tables in Entity Framework I want a generic method that takes an enum and creates a list of type objects and Name properties

To seed type tables in Entity Framework Core, I am rewriting this same code in the function 'StopLightColorTypeList' for every database type table that I need seed. I'm looking to create a generic method.
modelBuilder.Entity<StopLightColorType>
().HasData(WLProgramSeed.StopLightColorTypeList());
The StopLightColorTypeList takes each enumeration member creates a new StopLightColorType object and adds it to the list before:
Setting the object ID property to the int value of the Enum
Setting the Name property to the string value of the Enum
I am looking to use generics in such a way where I can
Specify the enumeration type (in this case it's StopLightColorsEnum)
Specify the return type object (in this case it's StopLightColorType)
Specify what property to set for the ID (must be integer) (in this case it's StopLightColorID)
Specify what property to set for the name (must be a string) (in this case it's StopLightColorName)
Optionally specify a function to parse the enumeration name
using System;
using System.Collections.Generic;
using System.Linq;
namespace GenericEnumToTypeList
{
class Program
{
static void Main(string[] args)
{
List<StopLightColorType> stopLightColorTypes = EFTools.StopLightColorTypes();
foreach(var stopLightColorType in stopLightColorTypes)
{
Console.WriteLine("ID: {0} Name: {1}", stopLightColorType.StopLightColorID, stopLightColorType.StopLightColorName);
}
Console.ReadLine();
}
}
public enum StopLightColorsEnum
{
Red = 1,
Yellow = 2,
Green = 3
}
public class StopLightColorType
{
public StopLightColorsEnum StopLightColorID { get; set; }
public string StopLightColorName { get; set; }
}
public class EFTools
{
public static List<StopLightColorType> StopLightColorTypes()
{
List<StopLightColorType> stopLightColorTypes = new List<StopLightColorType>();
foreach(StopLightColorsEnum stopLightColor in System.Enum.GetValues(typeof(StopLightColorsEnum))
{
StopLightColorType stopLightColorType = new StopLightColorType
{
StopLightColorID = stopLightColor,
StopLightColorName = ParseCapitalizedEnumName(stopLightColor.ToString())
};
stopLightColorTypes.Add(stopLightColorType);
}
return stopLightColorTypes;
}
public static string ParseUnderScoreName(string stringName)
{
return stringName.Replace("_", " "); ;
}
public static string ParseCapitalizedEnumName(string enumName)
{
string typeName = enumName[0].ToString();
for(int i = 1; i <= enumName.Length; i++)
{
if(Char.ToUpper(enumName[i]) == enumName[i])
{
typeName = typeName + " " + enumName[i].ToString();
}
}
return typeName;
}
}
}
I re-write this code for each database type, I'm looking for a generic to take care of this operation.
The core function you need is this:
IEnumerable<TResult> ListEnumMembers<TEnum, TResult>(string valueName, string nameName)
where TEnum : System.Enum
{
var enumInfos = Enum.GetValues(typeof(TEnum)).Cast<int>()
.Zip(Enum.GetNames(typeof(TEnum)), (i, s) => (Value: i, Name: s));
var typ = typeof(TResult);
var piValue = typ.GetProperty(valueName);
var piName = typ.GetProperty(nameName);
return enumInfos.Select(i =>
{
var instance = (TResult)Activator.CreateInstance(typ);
piValue.SetValue(instance, i.Value);
piName.SetValue(instance, i.Name);
return instance;
});
}
With this generic result type...
class EnumInfo
{
public int Value { get; set; }
public string Name { get; set; }
}
...and this code:
var enumInfos = ListEnumMembers<DayOfWeek, EnumInfo>("Value", "Name").ToList();
You get this result:
Value
Name
0
Sunday
1
Monday
2
Tuesday
3
Wednesday
4
Thursday
5
Friday
6
Saturday
I guess this will put you on the right track. Of course the code needs a couple of checks for the property names and types.

Cannot assign to ' ' because it is a 'method group' beginner

I'm practicing on setters and getters, got this error message:
Cannot assign to 'GetnewName' because it is a'method group'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Program
{
public static void Main(string[] args)
{
Bird b = new Bird();
b.GetnewName = "Tweety";
b.Chirp();
Bird b2 = new Bird();
b2.GetnewName = "Woody";
b2.Chirp();
}
}
public class Bird
{
private string name;
private double weight = 30.5d;
public void SetName(string newName)
{
if (newName != null && newName.Length > 2)
{
System.Console.WriteLine("Bird already has a name");
this.name = newName;
}
else if (newName.Length < 3)
{
System.Console.WriteLine("New name must be longer than two chars");
}
else
{
name = newName;
}
}
public string GetnewName()
{
return this.name;
}
public void Chirp()
{
System.Console.WriteLine(name + " says chirp!");
}
}
}
You need to set name using the SetName method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Program
{
public static void Main(string[] args)
{
Bird b = new Bird();
b.SetName("Tweety");
b.Chirp();
Bird b2 = new Bird();
b2.SetName("Woody");
b2.Chirp();
Console.ReadLine();
}
}
public class Bird
{
private string name;
private double weight = 30.5d;
public void SetName(string newName)
{
if (newName != null && newName.Length > 2)
{
System.Console.WriteLine("Bird already has a name");
this.name = newName;
}
else if (newName.Length < 3)
{
System.Console.WriteLine("New name must be longer than two chars");
}
else
{
name = newName;
}
}
public string GetnewName()
{
return this.name;
}
public void Chirp()
{
System.Console.WriteLine(name + " says chirp!");
}
}
}
You are using GetnewName as if it were a property, but you defined it as a pair of methods.
Property syntax combines the getter and the setter under a single name. Then C# re-routs assignments to the setter, and reads to the getter method:
public string Name {
get => name
set {
if (value != null && value.Length > 2) {
System.Console.WriteLine("Bird already has a name");
name = value;
} else if (value.Length < 3) {
System.Console.WriteLine("New name must be longer than two chars");
} else {
name = value;
}
}
}
Note: get => name above uses the new syntax. Old syntax for the same was get { return name; }
You cannot assign a string to a method, this causes the error.
I c# we don't use getters and setters as in Java or C++. Instead we have properties which we use. You can assign and read values as if they where fields but they have special methods called accessors. You should refactor your class like this:
public class Bird
{
private string name;
private double weight = 30.5d;
public string Name
{
get => name;
set
{
if (value != null && value.Length > 2)
{
Console.WriteLine("Bird already has a name");
name = value;
}
else if (value != null && value.Length < 3)
{
Console.WriteLine("New name must be longer than two chars");
}
else
{
name = value;
}
}
}
public void Chirp()
{
System.Console.WriteLine(name + " says chirp!");
}
}
Then you can use it as:
var bird = new Bird();
// assign a value (equivalent to SetName method in your original code)
bird.Name = "Woody";
// read a value (equivalent to GetName method in your original code)
Console.WriteLine(bird.Name);
It seems like you want this:
public string Name
{
get; private set;
}
public void SetName(string newName)
{
if (newName != null && newName.Length > 2)
{
System.Console.WriteLine("Bird already has a name");
Name = newName;
}
else if (newName.Length < 3)
{
System.Console.WriteLine("New name must be longer than two chars");
}
else
{
Name = newName;
}
}
The get; will automatically return the value of your property, without the need for a private backing property, and typically you could use the setter inside the property to set the value, but since you need to pass a parameter too it, it seems like making the setter private and creating a separate method to actually set it would suit your needs.

Is there a var.contains - C#

I have a question about the following code:
private void Filter (object sender, Android.Text.TextChangedEventArgs e)
{
List<Animal> animalList = new List<Animal>();
if(!string.IsNullOrEmpty(_editText.Text))
{
foreach (string str in _animalList)
{
if (str.Contains(_editText.Text))
{
animalList.Add (str);
}
}
}
_listView.Adapter = new AnimalAdapter(this, _animalList = animalList);
}
The Animal class:
public class Animal
{
private readonly int _intKey;
public int AnimalNumber { get; private set; }
public int StableNumber { get; private set; }
public int LactoseNumber { get; private set; }
public Animal ( int intKey, int animalNumber, int stableNumber, int lactoseNumber )
{
_intKey = intKey;
AnimalNumber = animalNumber;
StableNumber = stableNumber;
LactoseNumber = lactoseNumber;
}
public override string ToString ()
{
return "Number: " + AnimalNumber + "\nGroup: " + StableNumber + "\nLactation: " + LactoseNumber;
}
}
Declaration of _animalList:
private List<Animal> _animalList;
i need to check if the _animalList Contains the input of the _editText.Text.
But _animalList isn't a string so i need to use a var.
Is there something like a var.Contains or do i have to use something else?
Contains method is available for string type. You will need to cast your object to string.
A/c to your class definition you should do like:
foreach (Animal str in _animalList)
{
if (str.ToString().Contains(_editText.Text)) //using user defined "ToString()"
{
animalList.Add (str);
}
}
You can also check individual properties:
foreach (Animal str in _animalList)
{
if (str.AnimalNumber.ToString().Contains(_editText.Text)) //if "AnimalNumber" is like "_editText.Text"
{
animalList.Add (str);
}
}
Instead of trying to filter using ToString, it would be better to use the real property values. For example:
var number = Convert.ToInt32(_editText.Text);
var filteredList = _animalList
.Where(x => x.AnimalNumber == number ||
x.StableNumber == number ||
x.LactoseNumber == number)
.ToList();
Otherwise, user could type "Number" and since your ToString override contains that string, all of the items in the list would match positively.
(I didn't include any validation or error checking in the code above, so you should consider those as well).
var inputText = _editText.Text;
int enteredNumber;
// you should make sure that the inputText is always an int
var isInt = int.TryParse(inputText, out enteredNumber);
//for example, if you are going to find by AnimalNumber, which is an int, you can use this. .
if (isInt){
foreach (var animal in _animalList){
var animalNumber = animal.AnimalNumber;
if (animalNumber == enteredNumber)
{
animalList.Add(animal);
}
}
}
Edit (LINQ alternative):
if (isInt){
animalList.AddRange(from animal in _animalList
let animalNumber = animal.AnimalNumber
where animalNumber == enteredNumber
select animal);
}
_animalList.Select(a => a.ToString()).Contains(_editText.Text)
This expression returns true if the output of the ToString method of any animal object equals _editText.Text.
_animalList.Select(a => a.ToString()).Any(str => str.Contains(_editText.Text))
This expression returns true if the output of the ToString method of any animal object contains _editText.Text (as a substring). This is equivalent to Shaharyar's answer.
var animalList = _animalList.Where(a => a.ToString().Equals(_editText.Text)).ToList();
var animalList = _animalList.Where(a => a.ToString().Contains(_editText.Text)).ToList();
These statements filter the input list directly.

Making multiple calls in unit test without using a for loop

Lets say I have a unit test similar to the below, is there a way to write one unit test rather than several but also avoid having a for loop in the unit test?
[Test]
public void RunTestWithMultipleOptions()
{
MyClass code = new MyClass();
code.Prefix = "{DS1}"; //Options are {DS1}, {DS2}, {DS3}, {DS4}
//Property could be set to
//code.Prefix = "{DS1}{DS2}";
//code.Prefix = "{DS1}{DS2}{DS3}";
//And so on
//Based on how many {DS} used a method needs calling
code.InputDataStore(1,"Data1");
//If used {DS1}{DS2} in Prefix then
//code.InputDataStore(1,"Data1");
//code.InputDataStore(2,"Data2");
//If used {DS1}{DS2}{DS3} in Prefix then
//code.InputDataStore(1,"Data1");
//code.InputDataStore(2,"Data2");
//code.InputDataStore(3,"Data3");
string OutputData = String.Empty;
code.Output += delegate(int Id, string Data)
{
if (Id == (int)OutputsEnum.OutputModified)
OutputData = Data;
};
//Call the input method which will raise the Output event which we can assert against
code.Input("hi there");
//Assert that output has replace the prefix {DS} with the data in the datastorecontent list
Assert.AreEqual("Data1hi there", OutputData);
}
I can pass in the property value to the unit test method and use test cases but based on what the property is MyMethod needs to be called x number of times. Without putting a loop in the test I can't think of a way without having all the permetations as separate unit tests.
UPDATE: Here is the main contents of the class:
public event Action<int, string> Output;
public string Prefix { get; set; }
public string Postfix { get; set; }
private List<string> DataStoreContents = new List<string>() { "", "", "", "" };
public void Input(string Data)
{
if (Output != null)
{
if (!String.IsNullOrEmpty(Prefix))
{
Prefix = Prefix.Replace("{DS1}", DataStoreContents[0]);
Prefix = Prefix.Replace("{DS2}", DataStoreContents[1]);
Prefix = Prefix.Replace("{DS3}", DataStoreContents[2]);
Prefix = Prefix.Replace("{DS4}", DataStoreContents[3]);
}
if (!String.IsNullOrEmpty(Postfix))
{
Postfix = Postfix.Replace("{DS1}", DataStoreContents[0]);
Postfix = Postfix.Replace("{DS2}", DataStoreContents[1]);
Postfix = Postfix.Replace("{DS3}", DataStoreContents[2]);
Postfix = Postfix.Replace("{DS4}", DataStoreContents[3]);
}
Output((int)OutputsEnum.OutputBeforeModified, Data);
Output((int)OutputsEnum.OutputModified, Prefix + Data + Postfix);
Output((int)OutputsEnum.OutputAfterModified, Data);
}
}
}
public void InputDataStore(int DataStore, string Data)
{
if (DataStore < 1 || DataStore > 4)
throw new ArgumentOutOfRangeException("Datastore number out of range");
DataStoreContents[DataStore - 1] = Data;
}
}
I want to test that when I call InputDataStore(1,"MyData1"); InputDataStore(2, "MyData"); that Output does in fact replace the relevant {DS1} values with the relevant string and also combine it with any other {DS} values
One options is to test each call apart and all of them together. If you test A&B&C, you can limit yourself to testing A,B,C apart and A&B&C together. One option is the next code(made some assumptions):
[TestFixture]
public class ToTestFixture
{
[SetUp]
public void SetUp()
{
_instance = new ToTest();
_instance.InputDataStore(1, "1");
_instance.InputDataStore(2, "2");
_instance.InputDataStore(3, "3");
_instance.InputDataStore(4, "4");
}
private ToTest _instance;
[TestCase("{DS1}","1")]
[TestCase("{DS2}", "2")]
[TestCase("{DS3}", "3")]
[TestCase("{DS4}", "4")]
[TestCase("{DS1}{DS2}{DS3}{DS4}", "1234")]
[Test]
public void TestPrefixReplacements(string input, string expectedResult)
{
_instance.Prefix = input;
//Call the input method which will raise the Output event which we can test
_instance.Input("Any string goes here as we test only prefix." );
Assert.AreEqual(expectedResult, _instance.Prefix);
}
}
internal enum OutputsEnum
{
OutputBeforeModified,
OutputModified,
OutputAfterModified
}
public class ToTest
{
public event Action<int, string> Output = (x, result) => Console.WriteLine(x.ToString() + result);
public string Prefix { get; set; }
public string Postfix { get; set; }
private List<string> DataStoreContents = new List<string>() {"1", "2", "3", "4"};
public void Input(string Data)
{
if (Output != null)
{
if (!String.IsNullOrEmpty(Prefix))
{
Prefix = Prefix.Replace("{DS1}", DataStoreContents[0]);
Prefix = Prefix.Replace("{DS2}", DataStoreContents[1]);
Prefix = Prefix.Replace("{DS3}", DataStoreContents[2]);
Prefix = Prefix.Replace("{DS4}", DataStoreContents[3]);
}
if (!String.IsNullOrEmpty(Postfix))
{
Postfix = Postfix.Replace("{DS1}", DataStoreContents[0]);
Postfix = Postfix.Replace("{DS2}", DataStoreContents[1]);
Postfix = Postfix.Replace("{DS3}", DataStoreContents[2]);
Postfix = Postfix.Replace("{DS4}", DataStoreContents[3]);
}
Output((int) OutputsEnum.OutputBeforeModified, Data);
Output((int) OutputsEnum.OutputModified, Prefix + Data + Postfix);
Output((int) OutputsEnum.OutputAfterModified, Data);
}
}
public void InputDataStore(int DataStore, string Data)
{
if (DataStore < 1 || DataStore > 4)
throw new ArgumentOutOfRangeException("Datastore number out of range");
DataStoreContents[DataStore - 1] = Data;
}
}
Anyhow I feel there is a bond between "DS1" and the index of the array. (1-0, 2-1). This means next refactoring is possible:
Prefix = Prefix.Replace("{DS"+index+"}", DataStoreContents[index-1]);
More than that I guess output action decision is strange and two if-s are duplicate code. This is what I mean:
public void Input(string Data)
{
if (Output != null)
{
Prefix = ApplyReplaceRules(Prefix);
Postfix = ApplyReplaceRules(Postfix);
Output((int) OutputsEnum.OutputBeforeModified, Data);
Output((int) OutputsEnum.OutputModified, Prefix + Data + Postfix);
Output((int) OutputsEnum.OutputAfterModified, Data);
}
}
private string ApplyReplaceRules(string patternString)
{
if (!String.IsNullOrEmpty(Postfix))
{
patternString = patternString.Replace("{DS1}", DataStoreContents[0]);
patternString = patternString.Replace("{DS2}", DataStoreContents[1]);
patternString = patternString.Replace("{DS3}", DataStoreContents[2]);
patternString = patternString.Replace("{DS4}", DataStoreContents[3]);
}
return patternString;
}

Reflection in C#

I have recently started a development in c# and want to use reflection in following situation.
If I have a Enum class as
Enum Operation
{
Read=0;
Write;
}
If I give input as
String str = "Operation.Write";
I shoud be able to get output as 1;
Or
if constants are defined like
const int Read=0;
const int Write=1;
If the input is
String str = "Read";
output should be 0
Please Help.
You can use Enum.Parse to have that functionality.
If we combine your proposals we can get something like this.
public static Operation getOperationByName(String name) {
return Enum.Parse(typeof(Operation),name);
}
Where the name should not be null and represent the name or position in enum ie
"Read" will return Operation.Rerad and "1" will return Operation.Write
Heres the complete code to also Get the type of the Enum through Reflection without hardcoding it. The ParseConstant Method is also generic, s.t. you can use if for every Type.
namespace MyNamgespace
{
public enum Operation
{
Read = 0,
Write
}
public class ClassWithConstants
{
public const int Read = 0;
public const int Write = 1;
}
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine((ParseEnum("Operation.Write")));
Console.WriteLine((ParseContant<ClassWithConstants>("Write")));
Console.ReadLine();
}
static int ParseEnum(string enumValue)
{
var typeName = enumValue.Split('.')[0];
var valueName = enumValue.Split('.')[1];
var enumType = Type.GetType(string.Format("MyNamespace.{0}", typeName));
var op = (Operation) Enum.Parse(enumType, valueName);
return (int)op;
}
static int ParseContant<T>(string constantName)
{
var type = typeof (T);
var field = type.GetField(constantName, BindingFlags.Static | BindingFlags.Public);
return (int)field.GetValue(null);
}
}
}
var name = Enum.GetName(typeof(Operation), Operation.Write) //name = 'Write'
var value = Enum.Parse(typeof(Operation), "Write") //value = Operation.Write

Categories