繁体   English   中英

自定义类型中C#系统类型的转换

[英]Conversion of C# system type in a custom type

它的 C# 类库项目。 类只有一个返回字典的函数,

//dictionary definition 
Dictionary<string, int> stockLengths = new Dictionary<string, int>();
    stockLengths.Add("Length_5000", 0);
    stockLengths.Add("Length_3000", 0);

//return statement
Value result = new Value(stockLengths);
    return result;

系统调用类库不接受字典并出现以下错误:

"使用配置器类型而不是 System.Type System.Collections.Generic.Dictionary`2[System.String,System.Int32] "

这里 Configurator = 一个自定义软件,它调用类库 dll...

我在这里缺少什么,可以将字典的系统类型转换为任何自定义类型,以便主机系统可以接受返回的数据类型...

// Value struct

 public struct Value : IEquatable<Value>
    {
        public static readonly Value Null;

        public Value(string value);
        public Value(decimal? value);
        public Value(decimal value);
        public Value(bool? value);
        public Value(bool value);
        public Value(IExpressionValue value);
        public Value(IExpressionValueCollection value);
        public Value(IExpressionValueArray value);
        public Value(DataType valueType);
        public Value(object value);

        public object Data { get; }
        public bool HasValue { get; }
        public Type ValueType { get; }
        public DataType Type { get; }

        public IExpressionValueArray AsArray();
        public bool AsBoolean();
        public IExpressionValueCollection AsCollection();
        public bool? AsNullableBoolean();
        public decimal? AsNullableNumber();
        public string AsNullableString();
        public decimal AsNumber();
        public string AsString();
        public Value Clone();
        public Value ConvertTo(DataType type);
        public bool ConvertToBoolean();
        public bool? ConvertToNullableBoolean();
        public decimal? ConvertToNullableNumber();
        public string ConvertToNullableString();
        public decimal ConvertToNumber();
        public string ConvertToString();
        public override bool Equals(object obj);
        public bool Equals(Value other);
        public override int GetHashCode();
        public string GetTraceString();
        public override string ToString();

        public static implicit operator Value(decimal? value);
        public static implicit operator Value(bool value);
        public static implicit operator Value(bool? value);
        public static implicit operator Value(decimal value);
        public static implicit operator Value(string value);
    }



public interface IExpressionValueCollection : IEnumerable<IExpressionValue>, IEnumerable
    {
        IExpressionValue this[string key] { get; }

        int Count { get; }
        IEnumerable<string> Keys { get; }

        bool Contains(string key);
        IExpressionValue GetOrCreateElement(string key, DataType valueType);
    }


public interface IExpressionValue
    {
        Value Value { get; }

        IExpressionValue GetProperty(string identifier);
        bool TryGetProperty(string identifier, out IExpressionValue property);
    }  


 public enum DataType
    {
        Unassigned = 0,
        Boolean = 1,
        String = 2,
        Number = 3,
        DateTime = 4,
        BooleanArray = 5,
        StringArray = 6,
        NumberArray = 7,
        CollectionArray = 8,
        Collection = 9,
        UnassignedArray = 10
    }

在此处输入图片说明

该解决方案在提供的文档中不可用,当我联系技术支持时,他们只提供了一个提示,然后通过更多的探索,我能够成功地将系统数据类型转换为主机软件支持的“THE”数据类型......在这里,我提供解决方案,以帮助像我这样的人:

    using TDCI.BuyDesign.Configurator.Engine;
    using TDCI.BuyDesign.Configurator.Engine.Expressions;
    using TDCI.BuyDesign.Configurator.Engine.RuleEngine;

            // sample code to return Collections 
            var returnCollection = new SimpleExpressionValueCollection();
            returnCollection.Add("Length_5000", 5000);
            returnCollection.Add("Length_3000", 3000);

            Value result = new Value(returnCollection);
            return result;
            
            // sample code to return arrays 
            var returnArrays = new SimpleExpressionValueArray(DataType.String);
            returnArrays.Add(new SimpleExpressionValue("Test Value 1"));
            returnArrays.Add(new SimpleExpressionValue("Test Value 2"));
            returnArrays.Add(new SimpleExpressionValue("Test Value 3"));

            Value result = new Value(returnArrays);
            return result;
            

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM