c# casting string to class object - c#

i want to cast a string from database to a class object without having to go over each possible outcome.
so not
if(type.StartsWith("ContactPersonType")){//} else if(type.StartsWith("ContactPersonTitle")){//}
this is what i have so far
private static T Create<T>(IDataRecord record)
{
var properties = typeof(T).GetProperties();
var returnVal = Activator.CreateInstance(typeof(T));
properties.ToList().ForEach(item =>
{
string type = item.GetMethod.ReturnParameter.ParameterType.Name;
if (type.StartsWith("ContactPerson"))
{
Type t = Type.GetType(item.GetMethod.ReturnParameter.ParameterType.ToString());
item.SetValue(returnVal, Convert.ChangeType(record[item.Name].ToString(), t));
}
else if (!type.StartsWith("ObservableCollection"))
{
item.SetValue(returnVal, Convert.ChangeType(record[item.Name].ToString(), item.PropertyType));
}
});
return (T)returnVal;
}
public class ContactPersonType
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
private String _name;
public String Name
{
get { return _name; }
set { _name = value; }
}
}
thanks

Use an anonymous collection when you want a certain action to apply to different input values.
foreach(var option in new[] {"ContactPerson", "ContactPersonTitle" }){
if (type.StartsWith(option))
{
Type t = Type.GetType(item.GetMethod.ReturnParameter.ParameterType.ToString());
item.SetValue(returnVal, Convert.ChangeType(record[item.Name].ToString(), t));
}
}

Related

C# class not taking value being pass for LINQ query

Hopefully this makes sense. I have a class named ShippingCont
In this class, I have a LINQ connection like below. I want to be use this class to call the given table and get all the necessary fields instead of calling individual queries to the DB.
public static ShippingContainerDataContext shippingContainer = new ShippingContainerDataContext();
public static SHIPPING_CONTAINER sc2 = shippingContainer.SHIPPING_CONTAINERs.FirstOrDefault(a => a.CONTAINER_ID == _externalContainerId);
private string _containerId = sc2.COMPANY;
private string _company = sc2.COMPANY;
public string fromProgram
{
get { return _externalContainerId; }
}
public string ContId
{
get { return sc2.CONTAINER_ID; }
set { _externalContainerId = value; }
}
public string _ContainerId
{
get { return sc2.CONTAINER_ID; }
set { _ContainerId = value; }
}
public string _Company
{
get { return sc2.COMPANY; }
set { _company = value; }
}
When I try to pass a value to the _externalContainerId in the class. The LINQ query returns no records and I get the error Object reference not set to an instance of an object.
I know the LINQ returns data because when I manually provide the container ID in the LINQ query like (see below), I get a result set.
public static SHIPPING_CONTAINER sc2 = shippingContainer.SHIPPING_CONTAINERs.FirstOrDefault(a => a.CONTAINER_ID == "00008878742000004419");
The value is being pass from the main program like below.
ShippingCont sc = new ShippingCont("00008878742000004419");
I know the value is being passed because when I call the fromProgram() the value prints.
What am I doing wrong?
Since sc2 is static, the query is probably running before _externalContainerId is set (_externalContainerId = null), which would return empty. It would then remain empty for the duration of the program, because it is not getting recalculated, causing an error when you try to access a member. Since you are setting _externalContainerId in the constructor, you might place the assignment of sc2 in there as well. E.g.
public ShippingCont(string id)
{
this._existingContainerId = id;
sc2 = shippingContainer.SHIPPING_CONTAINERs.FirstOrDefault(a => a.CONTAINER_ID == _externalContainerId);
}
public static ShippingContainerDataContext shippingContainer = new ShippingContainerDataContext();
public SHIPPING_CONTAINER sc2;
private string _containerId = sc2.COMPANY;
private string _company = sc2.COMPANY;
public string fromProgram
{
get { return _externalContainerId; }
}
public string ContId
{
get { return sc2.CONTAINER_ID; }
set { _externalContainerId = value; }
}
public string _ContainerId
{
get { return sc2.CONTAINER_ID; }
set { _ContainerId = value; }
}
public string _Company
{
get { return sc2.COMPANY; }
set { _company = value; }
}

How can I fix the issue with the input string in this cmdlet?

In the last month of so I have been trying to learn some C# with the aim of writing some PowerShell modules. I looked at some articles and documentation (Creating a client with C# - Microsoft.Management.Infrastructure) to try and put together a simple CIM cmdlet that would return the local network adapters.
The class library compiles okay, but when I run the command in PowerShell, it shows a format exception.
Show-LocalAdapter : Input string was not in a correct format.
In function based problems, I would normally see an issue with a line in the error reporting, but the error does not point me in the right direction with a cmdlet.
Hopefully someone here can help me as I have exhausted my, admittedly limited, knowledge on debugging this problem.
Here is the code for the cmdlet.
using System.Collections;
using System.Linq;
using System.Management.Automation;
using System.Text.RegularExpressions;
using Microsoft.Management.Infrastructure;
namespace NetTest
{
[Cmdlet(VerbsCommon.Show, "LocalAdapter")]
[OutputType(typeof(NetworkAdapter))]
public class ShowLocalAdapterCmdlet : PSCmdlet
{
private string[] _manufacturer;
private string _name;
private bool? _physicalAdapter;
private int _maxEntries = 100;
[Parameter(Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Alias("Vendor")]
public string[] Manufacturer
{
get { return this._manufacturer; }
set { _manufacturer = value; }
}
[Parameter(Position = 1, ValueFromPipelineByPropertyName = true)]
public string Name
{
get { return this._name; }
set { _name = value; }
}
[Parameter(Position = 2, ValueFromPipelineByPropertyName = true)]
public bool? PhysicalAdapter
{
get { return this._physicalAdapter; }
set { _physicalAdapter = value; }
}
[Parameter(Position = 3)]
public int MaxEntries
{
get { return this._maxEntries; }
set { _maxEntries = value; }
}
protected override void BeginProcessing()
{
base.BeginProcessing();
}
protected override void ProcessRecord()
{
CimSession session = CimSession.Create("localHost");
IEnumerable cimResults = session.QueryInstances(#"root\cimv2", "WQL", "SELECT * FROM Win32_NetworkAdapter");
var query = cimResults.Cast<CimInstance>().Select(ReturnNetworkAdapter);
// Filter Name
if (Name != null)
{
query = query.Where(adapter => adapter.Name != null && adapter.Name.StartsWith(Name));
}
// Manufacturer Filter
if (Manufacturer != null)
{
query = query.Where(
adapter =>
adapter.Manufacturer != null &&
Regex.IsMatch(adapter.Manufacturer.ToString(),
string.Format("^(?:{0})", string.Join("|", Manufacturer))));
}
// Physical Adapter: true or false
if (PhysicalAdapter != null)
{
query = query.Where(adapter =>
adapter.PhysicalAdapter == PhysicalAdapter);
}
// Return objects
query.Take(MaxEntries).ToList().ForEach(WriteObject);
}
private static NetworkAdapter ReturnNetworkAdapter(CimInstance item)
{
return new NetworkAdapter
{
Name = item.CimInstanceProperties["Name"].ToString(),
Description = item.CimInstanceProperties["Description"].ToString(),
DeviceId = int.Parse(item.CimInstanceProperties["DeviceId"].ToString()),
Manufacturer = item.CimInstanceProperties["Manufacturer"].ToString(),
NetConnectionId = item.CimInstanceProperties["NetConnectionId"].ToString(),
PhysicalAdapter = bool.Parse(item.CimInstanceProperties["PhysicalAdapter"].ToString())
};
}
}
}
Here is the class for the network adapter object.
namespace NetTest
{
public class NetworkAdapter
{
private string _name;
private string _description;
private int _deviceId;
private string _manufacturer;
private string _netConnectionId;
private bool _physicalAdapter;
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Description
{
get { return _description; }
set { _description = value; }
}
public int DeviceId
{
get { return _deviceId; }
set { _deviceId = value; }
}
public string Manufacturer
{
get { return _manufacturer; }
set { _manufacturer = value; }
}
public string NetConnectionId
{
get { return _netConnectionId; }
set { _netConnectionId = value; }
}
public bool PhysicalAdapter
{
get { return _physicalAdapter; }
set { _physicalAdapter = value; }
}
}
}
Calls like this item.CimInstanceProperties["Name"].ToString() is not what are you expecting. You should look at property Value:
private static NetworkAdapter ReturnNetworkAdapter(CimInstance item)
{
return new NetworkAdapter
{
Name = item.CimInstanceProperties["Name"].Value.ToString(),
Description = item.CimInstanceProperties["Description"].Value?.ToString(),
DeviceId = int.Parse(item.CimInstanceProperties["DeviceId"].Value.ToString()),
Manufacturer = item.CimInstanceProperties["Manufacturer"].Value?.ToString(),
NetConnectionId = item.CimInstanceProperties["NetConnectionId"].Value?.ToString(),
PhysicalAdapter = bool.Parse(item.CimInstanceProperties["PhysicalAdapter"].Value.ToString())
};
}

Custom Collection/List To Store Custom Objects

I have a customer object class:
public class customerObject
{
private string _address1;
private string _address2;
private string _address3;
private string _category;
private string _country;
private string _county;
private string _custcode;
private string _fullname;
private string _int_rep_hou;
private string _int_rep_key;
private double _lat;
private double _lng;
private string _postcode;
private string _rep_code;
private string _telephone;
public customerObject()
{
}
public string Address1
{
get { return _address1; }
set { _address1 = value; }
}
public string Address2
{
get
{
return _address2;
}
set { _address2 = value; }
}
public string Address3 { get { return _address3; } set { _address3 = value; } }
public string Category
{
get { return _category; }
set { _category = value; }
}
public string Country { get { return _country; } set { _country = value; } }
public string County { get { return _county; } set { _county = value; } }
public string Custcode
{
get { return _custcode; }
set { _custcode = value; }
}
public string Fullname
{
get { return _fullname; }
set { _fullname = value; }
}
public string Int_rep_hou
{
get { return _int_rep_hou; }
set { _int_rep_hou = value; }
}
public string Int_rep_key
{
get { return _int_rep_key; }
set { _int_rep_key = value; }
}
public double Lat { get { return _lat; } set { _lat = value; } }
public double Lng { get { return _lng; } set { _lng = value; } }
public string Postcode { get { return _postcode; } set { _postcode = value; } }
public string Rep_code
{
get { return _rep_code; }
set { Rep_code = value; }
}
public string Telephone { get { return _telephone; } set { _telephone = value; }
}
}
I have a CustomCollections class
public class CustomerCollection
{
public List<customerObject> Customers { get; set; }
}
My method that loops through dt rows and converts to a customer object
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//Create Collection Object
Valueobjects.CustomerCollection Collection = new Valueobjects.CustomerCollection();
foreach (DataRow row in temptablename.Rows)
{
//Create Customer Object
Valueobjects.customerObject Customer = new Valueobjects.customerObject();
//set values of customer object
Customer.Rep_code = "";
Customer.Int_rep_key = "";
Customer.Int_rep_hou = "";
Customer.Fullname = row["Fullname"].ToString().Trim();
Customer.Custcode = row["Custcode"].ToString().Trim();
Customer.Category = row["Category"].ToString().Trim();
Customer.Address1 = row["Address1"].ToString().Trim();
Customer.Address2 = row["Address2"].ToString().Trim();
Customer.Address3 = row["Address3"].ToString().Trim();
Customer.Postcode = row["Postcode"].ToString().Trim();
Customer.Country = row["Country"].ToString().Trim();
Customer.Telephone = row["Telephone"].ToString().Trim();
Customer.Lat = Convert.ToDouble(row["Lat"]);
Customer.Lng = Convert.ToDouble(row["Lng"]);
Customer.County = row["County"].ToString().Trim();
//add to the collection (list)
Collection.Customers.Add(Customer);
}
temptablename = null;
return Collection;
}
However when I create a new Customer object and a new CustomerCollection object I am getting an error when adding the customer to the collection list.
Error:
Error 32 Cannot implicitly convert type
'Classes.Valueobjects.CustomerCollection' to
'System.Collections.Generic.List'
Your method is returning a List<CustomerCollection>:
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//...
}
But the code is trying to return a CustomerCollection:
return Collection;
Just as the error says, these two types are different.
If a CustomerCollection is already a collection of customers, then semantically what is a List<Valueobjects.CustomerCollection>? A collection of collections? It seems like you're over-pluralizing your objects :)
There are two approaches here. Either return a CustomerCollection from the method:
public CustomerCollection dolist(DataTable temptablename)
{
//...
}
Or use a List<Customer> if you want to use generic lists as your collection containers:
public List<Customer> dolist(DataTable temptablename)
{
//...
var Collection = new List<Customer>();
//...
Collection.Add(Customer);
//...
return Collection;
}
Side note: You may want to stick to C# conventions for variable naming. As you can see from the code highlighting here on Stack Overflow, your variable names can easily be mistaken for classes/types, which can cause confusion when supporting the code.
Return a CustomerCollection instead of a List<Valueobjects.CustomerCollection>:
public Valueobjects.CustomerCollection Dolist(DataTable temptablename)
{
// ...
Your object has a list, it is not a list.
MSDN: Inheritance

Convert a class into an array

I have a DisplayedData class ...
public class DisplayedData
{
private int _key;
private String _username;
private String _fullName;
private string _activated;
private string _suspended;
public int key { get { return _key; } set { _key = value; } }
public string username { get { return _username; } set { _username = value; } }
public string fullname { get { return _fullName; } set { _fullName = value; } }
public string activated { get { return _activated; } set { _activated = value; } }
public string suspended { get { return _suspended; } set { _suspended = value; } }
}
And I want to to put the objects from this class into an array where all objects inside of this class should be converted into an String[]
I have..
DisplayedData _user = new DisplayedData();
String[] _chosenUser = _user. /* Im stuck here :)
or can I create an array where all the items inside are consist of variables of different datatype so that the integer remains an integer and so the strings too?
You can create an array "with your own hands" (see Arrays Tutorial):
String[] _chosenUser = new string[]
{
_user.key.ToString(),
_user.fullname,
_user.username,
_user.activated,
_user.suspended
};
Or you could use Reflection (C# Programming Guide):
_chosenUser = _user.GetType()
.GetProperties()
.Select(p =>
{
object value = p.GetValue(_user, null);
return value == null ? null : value.ToString();
})
.ToArray();

C# - Marshall by value problem!

Here is the thing, I have a problem creating a new object using the remote mechanism "marshal by value".
Here is my class:
[Serializable]
internal class Empleado_MBV
{
public Empleado_MBV()
{
Id = 123456789;
Nombres = "NotEntry";
Apellidos = "NotEntry";
FechaNacimiento = DateTime.MinValue;
Direccion = "NotEntry";
Metapreferencias = "NotEntry";
}
private List<Multas> _multas;
internal List<Multas> Multas
{
get { return _multas; }
set { _multas = value; }
}
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _nombres;
public string Nombres
{
get { return _nombres; }
set { _nombres = value; }
}
private string _apellidos;
public string Apellidos
{
get { return _apellidos; }
set { _apellidos = value; }
}
private DateTime _FecNac;
public DateTime FechaNacimiento
{
get { return _FecNac; }
set { _FecNac = value; }
}
private string _direccion;
public string Direccion
{
get { return _direccion; }
set { _direccion = value; }
}
private string _metapreferencias;
public string Metapreferencias
{
get { return _metapreferencias; }
set { _metapreferencias = value; }
}
public string _AppDomainHost
{
get { return AppDomain.CurrentDomain.FriendlyName.ToString(); }
}
}
But when I try to create an object in another "appdomain", the property "_AppDomainHost" of "Empleado" does not show the "appdomain" I had created, but show the "appdomain" by default. Some ideas?
AppDomain ad1 = AppDomain.CreateDomain("NewAppDomain");
//Crear new object in my new AD.
Empleado_MBV mbv_emp = (Empleado_MBV)ad1.CreateInstanceFromAndUnwrap("DEMO_MBV_MBR.exe", "DEMO_MBV_MBR.Empleado_MBV");
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName.ToString());
Console.WriteLine("MBV : {0}",mbv_emp._AppDomainHost.ToString());
Console.ReadLine();
Result:
DEMO_MBV_MBR.vshost.exe
MBV : DEMO_MBV_MBR.vshost.exe
The result that I want:
DEMO_MBV_MBR.vshost.exe
MBV : NewAppDomain
You need to store AppDomain in Empleado_MBV's constructor.
What you are doing right now is displaying current AppDomain using its Current static property. It will return the AppDomain where current code is being executed.
Example:
private string _appDomainHost;
public string _AppDomainHost
{
get { return _appDomainHost; }
}
and in constructor:
_appDomainHost = AppDomain.CurrentDomain.FriendlyName.ToString();

Categories