sebastian.roibu
sebastian.roibu

Reputation: 2859

Accessing the Name attribute of an object

I have this code. I want to access the "Name" attribute of the object because type contains something like

{Name="String", FullName="System.String"}

but i want only "string".

DataTable dt = dr.GetSchemaTable();
foreach (DataRow myField in dt.Rows){
   var name = myField["ColumnName"];
   var type = myField["DataType"];
   Console.Out.WriteLine("type = " + type.toString());
}

ideas ?

Upvotes: 0

Views: 88

Answers (2)

CodeCaster
CodeCaster

Reputation: 151594

Don't call type.ToString() but ((Type)type).Name.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

You could cast to the corresponding type and then access the Name property:

var type = (Type)myField["DataType"];
Console.WriteLine("type = " + type.Name);

Upvotes: 4

Related Questions