5

我有一个名为 IAuditable 的接口,如下所示:

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

AuditTrail 类中的两个通用方法如下:

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

以及我有一个属性可以通过在运行时获取一个类来返回审计摘要,如下所示:

 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 "";
        }
    }

如您所知,我们可以在运行时在特定库中找到类,请注意我不想在运行时调用泛型方法,只想调用带有创建的泛型方法,换句话说,我如何将特定类传递给 GetActualNewValue <...>() 作为 T,

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

此代码完美运行,但主要问题是 DTClient 必须在运行时找到并将 GetActualValue 定义为 T。

谢谢大家。

4

2 回答 2

0

您可以使用静态方法在参数中传递旧对象和更改对象来跟踪更改。

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
            }
        }
    }             
}
于 2014-07-25T09:33:20.257 回答
0

尝试这个:

    AuditTrail auditTrail = new AuditTrail();

    var method = auditTrail.GetType().GetMethod("GetActualOldValue");
    var genericMethod = method.MakeGenericMethod(auditTrail.GetType());
    var result = genericMethod.Invoke(auditTrail, null);
于 2013-08-12T05:52:22.797 回答