简体   繁体   中英

Conversion of C# system type in a custom type

Its C# class library project. Class has only one function which returns a dictionary,

//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;

the system calling class library is not accepting the dictionary with following error:

" Use Configurator Types instead of System.Type System.Collections.Generic.Dictionary`2[System.String,System.Int32] "

Here Configurator = A custom software, which is calling the class library dll...

What am i missing here, can convert a system type of dictionary into any custom type so that the host system can accept data type being returned...

// 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 solution was not available in provided docs, when I contacted technical support they only offered a tip and then with a little bit more exploring I was able to successfully convert the system data type into "THE" data type which host software support... Here I am providing solution, to help out someone like me:

    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;
            

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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