English 中文(简体)
是否有办法将这种投放在“c#”上
原标题:Is there a way to do this kind of casting in a c# predicate

我试图制定一种通用的方法,这种方法将回头,以在XML文件中找到内容。

基本如此:

private static Func<XElement, bool> GetPredicate<T>(Criterion criterion)
{
    switch (criterion.CriteriaOperator)
    {
        case CriteriaOperator.Equal:
            return x => (T)x.Attribute(criterion.PropertyName) == 
                (T)(criterion.PropertyValue);
        case CriteriaOperator.GreaterThan:
            return x => (T)x.Attribute(criterion.PropertyName) > 
                (T)(criterion.PropertyValue);
        case CriteriaOperator.GreaterThanOrEqual:
            return x => (T)x.Attribute(criterion.PropertyName) >= 
                (T)(criterion.PropertyValue);
        case CriteriaOperator.LessThan:
            return x => (T)x.Attribute(criterion.PropertyName) < 
                (T)(criterion.PropertyValue);
        case CriteriaOperator.LessThanOrEqual:
            return x => (T)x.Attribute(criterion.PropertyName) <= 
                (T)(criterion.PropertyValue);
        case CriteriaOperator.NotEqual:
            return x => (T)x.Attribute(criterion.PropertyName) != 
                (T)(criterion.PropertyValue);
        default:
            throw new ArgumentException("Criteria Operator not supported.");
    }
} 

只有这样才汇编。 问题载于<代码>x。 沥青。 PropertyName) part where the codificationer indicated:

Cannot cast expression of type System.Xml.Linq.XAttribute to type T

目前,我有两种方法相同,只有两种方法可以翻一番,另一种方法可以ci。 我真的不想重复。

最佳回答
问题回答

如果你用<代码>dnamic的代谢物取代<代码>T,则届时会奏效。 我对在这里放弃类型安全感到不舒服,因为你可能无法确保XML特性中的 st子是任何正确之类——因此,这种类型的安全是一种幻觉。

T 是否应该? <代码>XAttribute<>/code>,除非对通用格式有某种限制,否则不得转换成。 无论如何,你可能希望获得<代码>Attribute()。 价值,即示意图。 然后,你可以进行比较。 什么是<代码>厄立特里亚。 PropertyValue?

如果你的XML包含像数字一样的原始内容,那么你只能直截了当。 你们需要使用<代码>等方法。 TryParse(。 不幸的是,我没有办法限制使用<代码>TryParse方法的一般规定。 如果有的话,你可以说<代码>T.TryParse。 但是,没有任何办法,因此你可以 t。 普通人可能赢得帮助。

页: 1 Ts using =, but object.Equals ( should work.

为了进行转换,您可使用Convert.ChangeType():

case CriteriaOperator.Equal:
    return x => object.Equals(
        Convert.ChangeType(x.Attribute(criterion.PropertyName).Value, typeof(T)),
        criterion.PropertyValue);

问题在于,XML在某些情况下使用不同的换算规则(例如Double.Positivedley作为INF

为解决这一问题,您可使用http://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.aspx” rel=“nofollow”>XmlConvert, 转业者内部使用。 除此以外,还有“遗传”方法,如<条码>。

private static object Convert(string value, Type targetType)
{
    if (targetType == typeof(double))
        return XmlConvert.ToDouble(value);

    …

    throw new ArgumentException();
}

…

case CriteriaOperator.Equal:
    return x => object.Equals(
        Convert(x.Attribute(criterion.PropertyName).Value, typeof(T)),
        criterion.PropertyValue);

http://www.un.org

 GetPredicate<T>(Criterion criterion) where T : XAttribute




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签