Saturday, October 6, 2012

create properties for the class at runtime in WCF c#

create properties for the class at runtime in WCF c#

These are the methods i need wrote:

List<Test> test = new List<Test>(); public List<Test> GetData(int ReportId)      {        StringBuilder qr = new StringBuilder();        StringBuilder SQL = new StringBuilder();        DataTable dt;    qr.AppendLine(" select SQL");   qr.AppendLine(" from  V_REPORT_SQL f");   qr.AppendLine(String.Format(" where REPORT_ID = '{0}'", ReportId));     dt = DataManager.GetDatadt(qr);       foreach (DataRow dr in dt.Rows)          {            SQL.Append(dr[0].DBStr());          }          dt = DataManager.GetData(SQL);         foreach (DataRow dr in dt.Rows)          {         Test t = new Test()         {          can i dynamically create and iterate over the properties and assign the datarow?          };          test.Add(t);     }     return test;      } 

//This is the DataContract

 [DataContract]     public class Test     {       } 

Table V_Report_SQL

reportid | SQL 1        | select id,Name from table1 2        | select acctid,Name,date from table2 3        | select Name,location,date,XX,XX,XX from table3 

the problem i am trying to solve is i am sending the reportid to get the sql dynamically and then i pass the sql and i get the data in DataTable. now i need to send it as list<Test>. Can i dynamically create properties for the class Test. for example :

     for report id 1       [DataMember]      public int Id { get; set; }       [DataMember]      public string Name { get; set; }       for report id 2        [DataMember]      public int acctid { get; set; }       [DataMember]      public string Name { get; set; }       [DataMember]      public DateTime date { get; set; } 

the client application that consumes the service is build on silverlight 4.

Let me know if i am not clear, i can update it. I am not sure about the route i chose to solve. is there any other way i could solve this problem.

Update:

I have updated,I no longer use dynamic, not sure this route will solve.

Answers & Comments...

Answer: 1

In general, it's a bad idea to try to use platform-specific types in a web service. For example, you wouldn't expect Java to understand the DataTable type.

I suspect you will have the same problem trying to return a dynamic object. This can't even be described by the WSDL of the service, so I doubt any caller would know what to do with such a return value.

In fact, I don't know how the service would even serialize such a thing.

by : John Saundershttp://stackoverflow.com/users/76337

Answer: 2

I think your intentions are noble but effectively you would create a constantly changing web service. Who would use it? If I were you I would return one of the following objects:

  1. 2D table of strings (first row would be a series of headers and the remaining ones would store values respectively)
  2. 1D table of strings (property name - property value pairs)
  3. A string (flat content of a CSV file)
  4. Binary CSV
  5. Dynamic XML that you'll have to crawl through on the client side

I obviously might be wrong assuming that you care about the textual values only, but since you are using a report table, I thought there is a fair chance that this is the case.

Hope this helps, Piotr

by : Piotr Justynahttp://stackoverflow.com/users/224612




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog