Common Methods - Code Duplication

I am having problems with generic methods.

I have two classes that are generated (they are exactly the same, but I cannot use the code to use the same class object).

here are the classes:

public class1 : SoapHttpClientProtocol {
    public partial class notificationsResponse {

        private ResponseType[] responsesField;

        private bool ackField;

        /// <remarks/>
        public ResponseType[] Responses {
            get {
                return this.responsesField;
            }
            set {
                this.responsesField = value;
            }
        }

        /// <remarks/>
        public bool Ack {
            get {
                return this.ackField;
            }
            set {
                this.ackField = value;
            }
        }
    }
}

public class2 : SoapHttpClientProtocol {
    public partial class notificationsResponse {

        private ResponseType[] responsesField;

        private bool ackField;

        /// <remarks/>
        public ResponseType[] Responses {
            get {
                return this.responsesField;
            }
            set {
                this.responsesField = value;
            }
        }

        /// <remarks/>
        public bool Ack {
            get {
                return this.ackField;
            }
            set {
                this.ackField = value;
            }
        }
    }
}

      

as you can see that class1 and class2 are the same; and since they are inline classes I must have duplication.

For that matter, I'm trying to call the update method with these types of classes as a parameter:

    private void UpdateMessageResponses<T>(T results)
    {
        T responses = (T)results;

        foreach (var accts in results.Responses)
        {
            int row = GetRowIdByAccountId(accts.ObjectId);
            if (row != -1)
            {
                TestResultsGrid["Status", row].Value = String.Format("{0} {1} - {2} - {3}", accts.ResponseDate, accts.ObjectType, accts.Message, accts.ObjectId);
            }
        }
    }

      

how can I properly attribute the results to access the properties of the results?

0


a source to share


2 answers


You need to define an interface for the Responses property, and then specify that T should implement that interface (and that your classes implement the interface).



+2


a source


I may be wrong, but I don't think you can do this solely with generics. The only way you could have a constraint on a generic type parameter, but since the members you want to access are not declared in the base class that is common to your two classes, this option is unfortunately excluded.



+1


a source







All Articles