Click here to Skip to main content
15,905,679 members
Home / Discussions / C#
   

C#

 
Questionnice to know that a generic parameter of a function constrained to 'struct can .. Pin
BillWoodruff10-Jun-18 5:00
professionalBillWoodruff10-Jun-18 5:00 
What say you, my esteemed mentors and peers ? This is non-news; bad news; worth writing up as tip-trick ?

Use a nullable of that same generic parameter as a return value: I was aware there was some behind the scenes compiler magic with a hidden 'HasValue flag in a nullable 'struct that allowed an instance to be assigned null, but I never thought I could use the non-nullable generic parameter in the way shown here:
/* required
using System;
using System.Collections.Generic;
using System.ComponentModel;
*/
public static class ConversionExtensions
{
    public enum ConversionType
    {
        SByte, Byte, Int16, Int32, Int64,
        UInt32, UInt64, Single, Double,
        Guid
    }

    static Dictionary<Type, TypeConverter> TypeToConverter = null;

    public static T? TryParse<T>(this string str) where T : struct
    {
        if (TypeToConverter == null)
        {
            TypeToConverter = new Dictionary<Type, TypeConverter>();

            foreach (string ct in Enum.GetNames(typeof(ConversionType)))
            {
                var t = Type.GetType("System." + ct);

                TypeToConverter.Add(t, TypeDescriptor.GetConverter(t));
            }
        }

        Type type = typeof(T);

        // using C# 7 syntax for in-line out variable
        if (TypeToConverter.TryGetValue(type, out TypeConverter tc))
        {
// tbd: use 'TypeConverter.CanConvertFrom here rather than try/catch
            try
            {
                return (T) tc.ConvertFromString(str);
            }
            catch
            {
                return null;
            }
        }
        else
        {
            throw new KeyNotFoundException($"this converter does not handle Type: {type.FullName}");
        }

        return null;
    }
}

«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12


modified 10-Jun-18 12:33pm.

AnswerRe: nice to know that a generic parameter of a function constrained to 'struct can .. Pin
Nathan Minier11-Jun-18 2:31
professionalNathan Minier11-Jun-18 2:31 
GeneralRe: nice to know that a generic parameter of a function constrained to 'struct can .. Pin
BillWoodruff11-Jun-18 5:33
professionalBillWoodruff11-Jun-18 5:33 
GeneralRe: nice to know that a generic parameter of a function constrained to 'struct can .. Pin
Nathan Minier13-Jun-18 8:21
professionalNathan Minier13-Jun-18 8:21 
QuestionDisplaying a grid of mixed controls from tabular data Pin Pin
Leif Simon Goodwin7-Jun-18 1:10
Leif Simon Goodwin7-Jun-18 1:10 
AnswerRe: Displaying a grid of mixed controls from tabular data Pin Pin
OriginalGriff7-Jun-18 1:19
mveOriginalGriff7-Jun-18 1:19 
GeneralRe: Displaying a grid of mixed controls from tabular data Pin Pin
Leif Simon Goodwin7-Jun-18 1:36
Leif Simon Goodwin7-Jun-18 1:36 
RantRe: Displaying a grid of mixed controls from tabular data Pin Pin
Richard Deeming7-Jun-18 2:12
mveRichard Deeming7-Jun-18 2:12 
GeneralRe: Displaying a grid of mixed controls from tabular data Pin Pin
OriginalGriff7-Jun-18 2:24
mveOriginalGriff7-Jun-18 2:24 
GeneralRe: Displaying a grid of mixed controls from tabular data Pin Pin
Leif Simon Goodwin7-Jun-18 2:46
Leif Simon Goodwin7-Jun-18 2:46 
GeneralRe: Displaying a grid of mixed controls from tabular data Pin Pin
Eddy Vluggen7-Jun-18 11:40
professionalEddy Vluggen7-Jun-18 11:40 
GeneralRe: Displaying a grid of mixed controls from tabular data Pin Pin
Leif Simon Goodwin7-Jun-18 2:38
Leif Simon Goodwin7-Jun-18 2:38 
GeneralRe: Displaying a grid of mixed controls from tabular data Pin Pin
Richard MacCutchan7-Jun-18 3:52
mveRichard MacCutchan7-Jun-18 3:52 
GeneralRe: Displaying a grid of mixed controls from tabular data Pin Pin
Leif Simon Goodwin7-Jun-18 21:34
Leif Simon Goodwin7-Jun-18 21:34 
QuestionC# COM dll with same name and same location to work for two projects/modules Pin
Member 117321396-Jun-18 21:31
Member 117321396-Jun-18 21:31 
QuestionRe: C# COM dll with same name and same location to work for two projects/modules Pin
Richard MacCutchan6-Jun-18 21:53
mveRichard MacCutchan6-Jun-18 21:53 
AnswerRe: C# COM dll with same name and same location to work for two projects/modules Pin
Member 117321396-Jun-18 22:57
Member 117321396-Jun-18 22:57 
GeneralRe: C# COM dll with same name and same location to work for two projects/modules Pin
Richard MacCutchan6-Jun-18 23:05
mveRichard MacCutchan6-Jun-18 23:05 
GeneralRe: C# COM dll with same name and same location to work for two projects/modules Pin
OriginalGriff6-Jun-18 23:19
mveOriginalGriff6-Jun-18 23:19 
Questioncant send file bigger that 1 megabit ( Server_client ) *(Thanks For helping fixed) Pin
Hussein Tb6-Jun-18 3:20
Hussein Tb6-Jun-18 3:20 
SuggestionRe: cant send file bigger that 1 megabit ( Server_client ) Pin
Richard MacCutchan6-Jun-18 3:56
mveRichard MacCutchan6-Jun-18 3:56 
AnswerRe: cant send file bigger that 1 megabit ( Server_client ) Pin
Gerry Schmitz6-Jun-18 7:08
mveGerry Schmitz6-Jun-18 7:08 
QuestionRe: cant send file bigger that 1 megabit ( Server_client ) Pin
Eddy Vluggen6-Jun-18 11:21
professionalEddy Vluggen6-Jun-18 11:21 
AnswerRe: cant send file bigger that 1 megabit ( Server_client ) Pin
Hussein Tb6-Jun-18 18:44
Hussein Tb6-Jun-18 18:44 
GeneralRe: cant send file bigger that 1 megabit ( Server_client ) Pin
Hussein Tb6-Jun-18 18:55
Hussein Tb6-Jun-18 18:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.