Skip to content

Instantly share code, notes, and snippets.

@vkocjancic
Created January 21, 2020 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkocjancic/3e8a6b3496c412a75b1c85a1d2ba1111 to your computer and use it in GitHub Desktop.
Save vkocjancic/3e8a6b3496c412a75b1c85a1d2ba1111 to your computer and use it in GitHub Desktop.
.NET Reflection issue
internal class MyObject
{
#region Properties
#region PropNumericA
private int _propNumericA;
private bool _fPropNumericA;
public bool IsPROP_NUMERIC_ANull()
{
return !_fPropNumericA;
}
public int PROP_NUMERIC_A
{
get
{
if (IsPROP_NUMERIC_ANull())
{
throw new MissingFieldException("PROP_NUMERIC_A");
}
return _propNumericA;
}
set
{
_propNumericA = value;
_fPropNumericA = true;
}
}
#endregion
#region PropNumericB
private int _propNumericB;
private bool _fPropNumericB;
public bool IsPROP_NUMERIC_BNull()
{
return !_fPropNumericB;
}
public int PROP_NUMERIC_B
{
get
{
if (IsPROP_NUMERIC_BNull())
{
throw new MissingFieldException("PROP_NUMERIC_B");
}
return _propNumericB;
}
set
{
_propNumericB = value;
_fPropNumericB = true;
}
}
#endregion
#region PropNumericC
private int _propNumericC;
private bool _fPropNumericC;
public bool IsPROP_NUMERIC_CNull()
{
return !_fPropNumericC;
}
public int PROP_NUMERIC_C
{
get
{
if (IsPROP_NUMERIC_CNull())
{
throw new MissingFieldException("PROP_NUMERIC_C");
}
return _propNumericC;
}
set
{
_propNumericC = value;
_fPropNumericC = true;
}
}
#endregion
#region PropNumericD
private int _propNumericD;
private bool _fPropNumericD;
public bool IsPROP_NUMERIC_DNull()
{
return !_fPropNumericD;
}
public int PROP_NUMERIC_D
{
get
{
if (IsPROP_NUMERIC_DNull())
{
throw new MissingFieldException("PROP_NUMERIC_D");
}
return _propNumericD;
}
set
{
_propNumericD = value;
_fPropNumericD = true;
}
}
#endregion
#region PropNumericE
private int _propNumericE;
private bool _fPropNumericE;
public bool IsPROP_NUMERIC_ENull()
{
return !_fPropNumericE;
}
public int PROP_NUMERIC_E
{
get
{
if (IsPROP_NUMERIC_ENull())
{
throw new MissingFieldException("PROP_NUMERIC_E");
}
return _propNumericE;
}
set
{
_propNumericE = value;
_fPropNumericE = true;
}
}
#endregion
#region PropStringA
private string _propStringA;
public string PROP_STRING_A
{
get
{
return _propStringA;
}
set
{
_propStringA = value;
}
}
#endregion
#region PropStringB
private string _propStringB;
public string PROP_STRING_B
{
get
{
return _propStringB;
}
set
{
_propStringB = value;
}
}
#endregion
#endregion
#region Public methods
public static DataTable GetTableDefinition()
{
var table = new DataTable("MyObject");
table.Columns.Add("PROP_NUMERIC_A", typeof(int));
table.Columns.Add("PROP_NUMERIC_B", typeof(int));
table.Columns.Add("PROP_NUMERIC_V", typeof(int));
table.Columns.Add("PROP_NUMERIC_D", typeof(int));
table.Columns.Add("PROP_NUMERIC_E", typeof(int));
table.Columns.Add("PROP_STRING_A", typeof(string));
table.Columns.Add("PROP_STRING_B", typeof(string));
return table;
}
#endregion
}
class Program
{
static void Main(string[] args)
{
var table = MyObject.GetTableDefinition();
var objects = new List<MyObject>();
for(var n = 0; n < 1000; n++)
{
objects.Add(new MyObject());
}
var sw = Stopwatch.StartNew();
foreach (var myObject in objects)
{
var row = table.NewRow();
var type = myObject.GetType();
var properties = type.GetProperties();
foreach (var info in properties)
{
try
{
row[info.Name] = info.GetValue(myObject, null);
}
catch
{
// do nothing
}
}
table.Rows.Add(row);
}
sw.Stop();
Console.WriteLine("Elapsed time: {0} ms", sw.ElapsedMilliseconds);
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment