Question

I am trying to convert the following piece of java code to C# and I am getting stuck at the point where I need to get the data type of the column. I am not too sure about the functions or procedure calls that I would be using.

.NET Code

namespace Workflow.DataTypes
{
    [Serializable]
    public class DataColumn1
    {
        private Type _dataTypes = null;
        private string _columnName = null;
        private string _defaultValue = null;

        public DataColumn1(string columnName, Type dataType)
            : this(columnName, typeof(string), null)
        {
        }

        public DataColumn1(string columnName, Type dataType, string defaultValue)
        {
            SetColumnName(columnName);
            SetDataType(dataType);
            SetDeafultValue(defaultValue);
        }

        public string GetDefaultValue()
        {
            return _defaultValue;
        }

        public bool IsDefaultValueDefined()
        {
            return _defaultValue != null && _defaultValue.Trim().Length > 0;
        }

        public void SetDeafultValue(string def)
        {
            _defaultValue = def;
        }

        public string GetColumnName()
        {
            return _columnName;
        }

        public void SetColumnName(string columnName)
        {
            this._columnName = _columnName;
        }

        public Type GetDataType()
        {
            return _dataTypes;
        }

        public void SetDataType(Type types)
        {
            this._dataTypes = _dataTypes;
        }

        public void SetDataType(string type)
        {
            try
            {
                SetDataType(type != null ? typeof (Type) : typeof (string));
            }
            catch (ArgumentException e)
            {
                // to do JavaUtils.enumToList(DataTypes.class)
                throw new ArgumentException("Unsupported data type: " + type + ". Supported types: " );
            }
        }

        public override string ToString()
        {
            return GetColumnName() + "[" + GetDataType() + "]" + (IsDefaultValueDefined() ? " (" + _defaultValue + ")" : "");
        }
    }
}

All I am looking for is how to return the column name and its data type. Thank you for taking the time to read through.

Était-ce utile?

La solution

I'm not sure if that's what you were asking for, but if you need a type's name, use

//short class name
typeof(T).Name
//full name with namespace
typeof(T).FullName
//namespace only
typeof(T).Namespace

typeof(T) is resolved at compilation, but if you want to get it dynamically at runtime, use object.GetType() for that.

EDIT

Ok, I see some mistakes in your code:

    public void SetColumnName(string columnName)
    {
        this._columnName = _columnName;
    }

should be

    public void SetColumnName(string columnName)
    {
        this._columnName = columnName;
    }

same with

        this._dataTypes = _dataTypes;

which should be

        this._dataTypes = dataTypes;

basically you were reassigning the same values in those methods, ignoring objects sent as parameter. And this will definately won't work:

public void SetDataType(string type)
{
    try
    {
        SetDataType(type != null ? typeof (Type) : typeof (string));
    }

I'm not sure what are you trying to achieve here, but typeof(Type) is Type, which I doubt you intended to use. Also, typeof(string) will return the type of string class, not the object send in parameter. typeof(type) (an object sent in parameter) will also return you a string type, because that's the parameter type. Try to avoid confusing names, that are similar to keywords, especially when moving from Java to C#, because standard naming convention can trick you out.

Autres conseils

Try with .GetType().FullName or with .GetType().Name if you do not want the namespace.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top