Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Using below link, i read HP ALM data using c#, In ALM some custom fields also there i am unable to read custom fileds, can you help us.


QC DATA PULLER using C#[^]3

Thank You!

What I have tried:

Using below code i am able to read all fields code.

foreach (IBug thisBug in bugL)
                        {
                            dsbug.Tables[0].Rows.Add(thisBug.ID, thisBug.Summary, thisBug.Status, thisBug.AssignedTo, thisBug.DetectedBy, thisBug.ToString(), thisBug.Priority);
}


In Entities, IBug class has below code.

[DefaultMember("Field")]
    [Guid("2AF970F7-6CCC-4DFB-AA78-08F689481F94")]
    [TypeLibType(4160)]
    public interface IBug : IBaseFieldExMail
    {
        [DispId(0)]
        dynamic this[string FieldName] { get; set; }
        [DispId(20)]
        string AssignedTo { get; set; }
        [DispId(12)]
        dynamic Attachments { get; }
        [DispId(5)]
        bool AutoPost { get; set; }
        [DispId(23)]
        List ChangeLinks { get; }
        [DispId(19)]
        string DetectedBy { get; set; }
        [DispId(13)]
        bool HasAttachment { get; }
        [DispId(22)]
        bool HasChange { get; }
        [DispId(11)]
        dynamic History { get; }
        [DispId(4)]
        dynamic ID { get; }


Using foreach (IBug thisBug in bugL), i am able to read all fields, except dynamic this[string FieldName]. dynamic this["field"] has custom fields, i need to read dynamic this value.

How to read below entity in class file.
dynamic this[string FieldName]
Posted
Updated 22-Jan-17 19:36pm
v3
Comments
Patrice T 22-Jan-17 12:47pm    
"i am unable to custom fileds"
Can you make it a sentence ?
[no name] 22-Jan-17 13:03pm    
Ask whomever wrote the code to help you. You might want to consider posting a description of an actual problem.
sampath1750 22-Jan-17 21:40pm    
Using foreach (IBug thisBug in bugL), i am able to read all fields, except below value
dynamic this[string FieldName].

How to read dynamic this["Field"] value in foreach loop

1 solution

First, you have posted the interface and not class code. Please update the question with the class code.

Second, the "field" you are trying to read is a actually an indexer. You can read about indexers here:MSDN[^]. From the link:
Quote:
Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.


Looking at the code you have posted, here is what I can assume might be done in the class:

C#
static class Program
{
    static void Main(string[] args)
    {

        BugClass classObj = new BugClass();

        classObj.Id = 1;
        classObj.Name = "First";

        int firstId = classObj["Id"];
        string firstName = classObj["Name"];

        Console.Read();
    }
}


public interface IBug
{
    dynamic this[string FieldName] { get; set; }

    int Id { get; set; }

    string Name { get; set; }

}

class BugClass:IBug
{

    public dynamic this[string FieldName]
    {
        get
        {
            return this.GetType().GetProperty(FieldName).GetValue(this);
        }
        set
        {
            this.GetType().GetProperty(FieldName).SetValue(this, value);
        }
    }

    public int Id
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }
}


Here, dynamic this itself does not really hold anything. It merely is an alternate way to play around with class properties. As long as you are reading all the values from the properties, your code should be fine.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900