5

I have an interface called IAuditable as follows:

public interface IAuditable
{
    string AuditSummary
    {
        get;
    }
    string AuditDetails
    {
        get;
    } 
}

And two generic method in AuditTrail class as follows:

public T GetActualOldValue<T>() where T : IAuditable
    {
        return FromXElement<T>(OldValue);
    }
    public T GetActualNewValue<T>() where T : IAuditable
    {
        return FromXElement<T>(NewValue);
    }

as well as i have one property to returning Audit Summary by getting a class at run time as follows:

 public string AuditSummary
    {
        get
        {
            if (HasNewValue || HasOldValue)
            {
                string libraryVal = GetLibraryValue();
                Assembly assembly = Assembly.Load(libraryVal);
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsClass == true)
                    {
                        if (type.FullName.EndsWith("." + ClassName))
                        {
                            /*Here i want to call GetActualNewValue<foundClass>().AuditSummary
                             * or GetActualOldValue<foundClass>().AuditSummary;
                             */
                        }
                   }
                }
            }
            return "";
        }
    }

As you know we can find class at run time in specific library, Please note i don't want to call a generic method at run time,just wanna call generic method with founded, in other words how can i pass a specific class to GetActualNewValue<...>() as T,

AuditTrail auditTrail = AuditTrail.GetAuditTrail(76);
string summmary = auditTrail.GetActualOldValue<DTClient>().AuditSummary;
string details = auditTrail.GetActualOldValue<DTClient>().AuditDetails;

This code work perfectly but the main problem is DTClient must find at run time and define as T for GetActualValue.

Thanks all.

1
  • Just so I understand, you're trying to iterate through types at runtime, place them into a variable, then use that variable as the type parameter on a generic method call?
    – Chris
    Aug 12, 2013 at 23:15

2 Answers 2

0

Try this:

    AuditTrail auditTrail = new AuditTrail();

    var method = auditTrail.GetType().GetMethod("GetActualOldValue");
    var genericMethod = method.MakeGenericMethod(auditTrail.GetType());
    var result = genericMethod.Invoke(auditTrail, null);
4
  • 1
    Your code just find a class at run time, As the code shows i have no problem with finding the class.
    – Aria
    Aug 12, 2013 at 6:59
  • Sorry i haven't understood your problem in the first go., i have updated the code, please try again hopefully it will help Aug 12, 2013 at 7:03
  • You're welcome, I've seen this solution before, Now how can i access to AuditSummary property from "result".
    – Aria
    Aug 12, 2013 at 7:26
  • Your solution doesn't work because i must define Type as T while calling GetActualNewValue method,Anyway i don't want to invoke generic method at run time.Thank you.
    – Aria
    Aug 12, 2013 at 9:06
0

You can use static method passing old object and changed object in parameters to track changes.

public class
{
    AuditClass.AuditingUserActivity(OldObject,ChangedObject);   
}

class AuditClass
{
    public static void AuditingUserActivity<T>(this T obj1,obj2)
    {
        PropertyInfo[] properties = typeof(T).GetProperties(); //Gets all properties exist in passed objects of both

        foreach (PropertyInfo pi in properties)
        {
            object value1 = typeof(T).GetProperty(pi.Name).GetValue(obj1, null); // gets value for each entity
            object value2 = typeof(T).GetProperty(pi.Name).GetValue(obj2, null);

            if (value1 != null && value2 != null)
            {
                //You can compare and check whether changes made or not
            }
        }
    }             
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.