| |

VerySource

 Forgot password?
 Register
Search
View: 653|Reply: 4

Super difficult question (to me), master invited

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-28 13:00:01
| Show all posts |Read mode
How to read out all the attributes in an entity class, and read out the values ​​of the attributes?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Hong Kong

Post time: 2020-2-23 20:30:02
| Show all posts
Use reflection
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-2-23 23:30:01
| Show all posts
Use reflection, please refer to:

string sClass = CCConvert.GetRequsetQueryString ("ClassName");
string sMethodName = "GetReports";
// Get class
Type oType = System.Type.GetType ("CallCenter.Report.Class." + SClass);
if (oType == null)
{
Response.Write ("Class:" + sClass + "Does not exist!");
return;
}

// Implementation
object oInst = Activator.CreateInstance (oType);
if (oInst == null)
{
Response.Write ("Class:" + sClass + "Does not exist!");
return;
}
// Get method
MethodInfo oMethod = oType.GetMethod (sMethodName);

if (oMethod == null)
{
Response.Write ("Method:" + sMethodName + "Does not exist!");
return;
}
int iParamsCount = oMethod.GetParameters (). Length;
string sParams = CCConvert.GetRequsetQueryString ("Params");
string [] ary = sParams.Split (',');
if (ary.Length! = iParamsCount)
{
Response.Write (String.Format ("The number of passed parameters {0} is not equal to the actual number of parameters {1} of the method {2}, please check", ary.Length.ToString (), oMethod.Name , iParamsCount.ToString ()));
return;
}
// Get attributes
PropertyInfo Pro = oType.GetProperty ("Title");
Ranch


this.sTitle = Pro.GetValue (oInst, null) .ToString ();
Ranch
FieldInfo fldFileName = oType.GetField ("sExcelFileName");
Ranch
Ranch

string sHTML = oMethod.Invoke (oInst, BindingFlags.Public, Type.DefaultBinder, ary, null) .ToString ();
string sFileName = fldFileName.GetValue (oInst) .ToString ();
//Response.Write(sFileName);
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-4 12:30:02
| Show all posts
reflection
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-4 20:30:01
| Show all posts
Class1 class1 = new Class1 ();
        PropertyInfo [] arrayinfo = class1.GetType (). GetProperties ();
        foreach (PropertyInfo info in arrayinfo)
        {
            object value = info.GetValue (class1, null);
        }
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list