I want to use a generic WriteList(List value) function to write a List using the BinaryWriter. Here is the code I am using:
public void WriteList<T>(List<T> value)
{
for (int i = 0; i < value.Count; i++)
{
_writer.Write(value[i]);
}
}
The error I am receiving is:
Error 1 The best overloaded method match for 'System.IO.BinaryWriter.Write(bool)' has some invalid arguments
Error 2 Argument 1: cannot convert from 'T' to 'bool'
The BinaryFormatter is absolutely not an option.
I really don't think you can avoid BinaryFormatter. Because type T can be any complex type and each instance of T can represent a huge graph of variables on memory.
so the only solution you have is to convert your instance of T to the byte[] format and the simplest solution for that is: BinaryFormatter
Actually the reason .Write() method only accept primitive types is that it knows how to convert them directly to byte[] (using Convert.ToXXX()) but there is no way it could guess that for generic type T.
As a work around you can define an interface like this:
public interface IBinarySerializable
{
byte[] GetBytes();
}
and then implement it in your class:
public class MyClass: IBinarySerializable
{
public int X {get;set;}
public byte[] GetBytes()
{
return BitConverter.GetBytes(X); // and anyother
}
}
and change your method to this:
public void WriteList<T>(List<T> value) where T:IBinarySerializable
{
for (int i = 0; i < value.Count; i++)
{
_writer.Write(value[i].GetBytes());
}
}
If you check out the docs for BinaryWriter you'll see it doesnt accept an argument of object (Writes primitive types), and the compiler is trying its best at an overload, and failing, since you can't cast your T to bool, or anything else that BinarwWriter would like.
You're going to have to convert your object into something the BinaryWriter will work with.
You can't pass a generic T to BinaryWriter.Write - it has overloads for many specific types (bool, byte, byte[], int, string, etc), but no generic one. So you'll need to do it yourself, in some way similar to the code below.
public void WriteList<T>(List<T> value)
{
for (int i = 0; i < value.Count; i++)
{
switch (Type.GetTypeCode(typeof(T))){
//_writer.Write(value[i]);
case TypeCode.Boolean:
_writer.Write((bool)(object)value[i]);
break;
case TypeCode.Byte:
_writer.Write((byte)(object)value[i]);
break;
case TypeCode.Char:
_writer.Write((char)(object)value[i]);
break;
case TypeCode.Decimal:
_writer.Write((decimal)(object)value[i]);
break;
case TypeCode.Double:
_writer.Write((double)(object)value[i]);
break;
case TypeCode.Single:
_writer.Write((float)(object)value[i]);
break;
case TypeCode.Int16:
_writer.Write((short)(object)value[i]);
break;
case TypeCode.Int32:
_writer.Write((int)(object)value[i]);
break;
case TypeCode.Int64:
_writer.Write((short)(object)value[i]);
break;
case TypeCode.String:
_writer.Write((string)(object)value[i]);
break;
case TypeCode.SByte:
_writer.Write((sbyte)(object)value[i]);
break;
case TypeCode.UInt16:
_writer.Write((ushort)(object)value[i]);
break;
case TypeCode.UInt32:
_writer.Write((uint)(object)value[i]);
break;
case TypeCode.UInt64:
_writer.Write((ulong)(object)value[i]);
break;
default:
if (typeof(T) == typeof(byte[]))
{
_writer.Write((byte[])(object)value[i]);
}
else if (typeof(T) == typeof(char[]))
{
_writer.Write((char[])(object)value[i]);
}
else
{
throw new ArgumentException("List type not supported");
}
break;
}
}
Related
Help please, I have this case:
switch(MyFoo()){
case 0: //...
break;
case 1: //...
break;
case 2: //...
break;
default:
// <HERE>
break;
}
As you can see the switch gets the value directly from a method without saving it as a variable.
Is it possible to get which value fires the default case?
For example if MyFoo() returns 7, how can I get that value?
I want to avoid to save the method result as a variable, is there a way to get the switch value from inside a case? Something like this:
default:
this.SwitchValue // <<--
break;
Thank you for reading,
~Saba
Is there a way to get the switch value from inside a case?
The only (proper) way is actually to store the result of MyFoo() in a variable.
var fooResult = MyFoo();
switch (fooResult)
{
case 0:
...
break;
...
default:
handleOthersCase(fooResult);
break;
}
This code is readable and understandable and have no extra cost (As #SheldonNeilson says: It's on the stack anyway).
Also, the MSDN first example about switch totally look like this. You can also find informations int the language specification.
You also can make your own switch based on a dictionary, but the only advantage I see is that you can use it for complex cases (any kind of object instead of string/int/...). Performance is a drawback.
It may look like this:
public class MySwitch<T> : Dictionary<T, Action<T>>
{
private Action<T> _defaultAction;
public void TryInvoke(T value)
{
Action<T> action;
if (TryGetValue(value, out action))
{
action(value);
}
else
{
var defaultAction = _defaultAction;
if (defaultAction != null)
{
defaultAction(value);
}
}
}
public void SetDefault(Action<T> defaultAction)
{
_defaultAction = defaultAction;
}
}
And be used like this:
var mySwitch = new MySwitch<int>();
mySwitch.Add(1, i => Console.WriteLine("one")); // print "one"
mySwitch.Add(2, i => Console.WriteLine("two")); // print "two"
mySwitch.SetDefault(i => Console.WriteLine("With the digits: {0}", i)); // print any other value with digits.
mySwitch.TryInvoke(42); // Output: "With the digits: 42"
Or based on this response, this:
public class MySwitch2<T>
{
private readonly T _input;
private bool _done = false;
private MySwitch2(T input)
{
_input = input;
}
public MySwitch2<T> On(T input)
{
return new MySwitch2<T>(input);
}
public MySwitch2<T> Case(T caseValue, Action<T> action)
{
if (!_done && Equals(_input, caseValue))
{
_done = true;
action(_input);
}
return this;
}
public void Default(Action<T> action)
{
if (!_done)
{
action(_input);
}
}
}
Can be used like that:
MySwitch2<int>.On(42)
.Case(1, i => Console.WriteLine("one"))
.Case(2, i => Console.WriteLine("two"))
.Default(i => Console.WriteLine("With the digits: {0}", i));
I can't see a reason as well why to use it like that but may be a work around will be like this:
int x;
switch ( x = MyFoo())
{
case 0: //...
break;
case 1: //...
break;
case 2: //...
break;
default:
var s = x; // Access and play with x here
break;
}
No, this isn't possible.
You can assign the value to variable inside switch, if you want to look like reinventing the wheel:
int b;
.....
switch (b = MyFoo())
{
case 1:
break;
case 2:
break;
default:
//do smth with b
break;
}
The easiest way is to save the result of MyFoo() as a variable.. But if you don't want to do that you could do:
switch(MyFoo()){
case 0: //...
break;
case 1: //...
break;
case 2: //...
break;
default:
this.SwitchCase = MyFoo();
break;
}
Although I would advise against this and say save the value as a variable to save your program the extra work.
Saving the value of MyFoo as a variable becomes more important the more complex the example gets as the value of MyFoo could have changed between the switch and default case.
Also this will only work where MyFoo has no side-effects and obviously must always return the same value for any given parameter.
for example the following would work:
Private int MyFoo()
{
return 3;
}
But the following would not:
private int MyFoo()
{
Random r = new Random();
return r.Next(5);
}
This is possible now.
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#property-patterns
Example:
int? TryGetColumnIndex(string columnName)
=> GetValue(columnName)
switch
{ var result when result > -1 => result, _ => new int?() };
result will capture the result of GetValue.
Even cooler, you can do propery checks.
i.e instead of when result > -1 you can even say when result.ToString().Length > 2 and such.
This question already has answers here:
C#: Getting size of a value-type variable at runtime?
(8 answers)
Closed 8 years ago.
I have a function that determines the field size (in bytes) of a record value. If it is a string, I use Length to return the number bytes. If it is not a string, I call another method that assigns the number of bytes using a switch.
Here is what I have:
private int getRecordFieldSize(PropertyInfo recordField,DataRecord dataRecord)
{
if (recordField.PropertyType.ToString() == "System.String")
{
return recordField.GetValue(dataRecord,null).ToString().Length;
}
else
{
int bytesOfPropertyType = getBytesBasedOnPropertyType(recordField.PropertyType.ToString());
return bytesOfPropertyType;
}
}
private int GetBytesBasedOnPropertyType(string propType)
{
switch(propType)
{
case "System.Boolean":
return 1;
case "System.Byte":
return 1;
case "System.SByte":
return 1;
case "System.Char":
return 1;
case "System.Decimal":
return 16;
case "System.Double":
return 8;
case "System.Single":
return 4;
case "System.Int32":
return 4;
case "System.UInt32 ":
return 4;
case "System.Int64":
return 8;
case "System.UInt64":
return 8;
case "System.Int16":
return 2;
case "System.UInt16":
return 2;
default:
Console.WriteLine("\nERROR: Unhandled type in GetBytesBasedOnPropertyType." +
"\n\t-->String causing error: {0}", propType);
return -1;
}
}
My question: Is there a way I can avoid using the switch statement to assign the bytes?
I feel like there should be some way to get the number of bytes using Reflection but I can't find anything on MSDN.
I am really new to C# so feel free to rip my code apart.
Thanks
Two possible solutions for you:
Marshal.SizeOf() method (http://msdn.microsoft.com/en-us/library/y3ybkfb3.aspx)
The sizeof keyword (http://msdn.microsoft.com/en-us/library/eahchzkf%28VS.71%29.aspx)
The latter though will still need a switch statement as it's not possible to do:
int x;
sizeof(x);
sizeof only works with explicitly stated types, e.g. sizeof(int)
So (1) is your better option in this case (and it will work for all types, not just those in your switch statement).
This may help
private int getRecordFieldSize(PropertyInfo recordField,DataRecord dataRecord)
{
if (recordField.PropertyType.ToString() == "System.String")
{
return recordField.GetValue(dataRecord,null).ToString().Length;
}
else
{
int bytesOfPropertyType = System.Runtime.InteropServices.Marshal.SizeOf(recordField.PropertyType);
return bytesOfPropertyType;
}
}
How to find if given string is Built In Types, which are aliases of predefined types in the System Namespace.
Ex: 1
Input = "System.Int32" (this is string)
OutPut : True(indicating it's Built-In Name)
Ex :2
Input = "xxx"
Output = False(indicating it's not Built-In Type name)
Any suggestion regarding this is highly appreciated.
Thanks in advance
Assuming you want to check if a type-name belongs to a sql-type. There's no property or method available in the framework.
But you can do what SqlParameter would do to infer conversion from Type to SqlDbType if it's not set explicitely.
Following method is directly derived from SqlParameter's InferSqlType:
public static bool IsConvertibleToSqlDbType(String type)
{
switch(type) {
case "System.Int64":
case "System.Data.SqlTypes.SqlInt64":
return true;
case "System.Boolean":
case "System.Data.SqlTypes.SqlBoolean":
return true;
case "System.String":
case "System.Data.SqlTypes.SqlString":
return true;
case "System.DateTime":
case "System.Data.SqlTypes.SqlDateTime":
return true;
case "System.Decimal":
case "System.Data.SqlTypes.SqlDecimal":
return true;
case "System.Double":
case "System.Data.SqlTypes.SqlDouble":
return true;
case "System.Byte[]":
case "System.Data.SqlTypes.SqlBinary":
return true;
case "System.Byte":
case "System.Data.SqlTypes.SqlByte":
return true;
case "System.Int32":
case "System.Data.SqlTypes.SqlInt32":
return true;
case "System.Single":
case "System.Data.SqlTypes.Single":
return true;
case "System.Int16":
case "System.Data.SqlTypes.SqlInt16":
return true;
case "System.Guid":
case "System.Data.SqlTypes.SqlGuid":
return true;
case "System.Money":
case "System.SmallMoney":
case "System.Data.SqlTypes.SqlMoney":
return true;
case "System.Object":
return true;
default:
return false;
}
}
public bool IsBuiltInType(string typeName)
{
return Type.GetType(typeName, false) != null;
}
I think this is what your looking for.
http://msdn.microsoft.com/en-us/library/system.type.gettype.aspx
IsBuiltInType("System.Int32");
The above returns true, as it finds the System.Int32 type - You can also throw an error if it doesn't find the specified type.
Type.GetType("System.Int32", false) != null would let you know if the type exists. But, it would also check for types loaded in current app domain.
I think you are looking for Assembly.GetType(string)
If the method returns null, the assembly does not know the type.
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettype.aspx
Then you would only need to know which assembly you want to check against.
In your case, it looks like the System assembly would suffice.
Jeppe makes a good point, see his comment below.
How about this:
static class TypeHelper
{
public static readonly IList<string> BuiltInTypeNames;
static TypeHelper()
{
Type[] allBuiltInTypes = { typeof(string), typeof(int), typeof(decimal), }; // add whatever types you consider "built-in".
BuiltInTypeNames = allBuiltInTypes.Select(t => t.FullName).ToList().AsReadOnly();
}
}
Then with an input string input you can say TypeHelper.BuiltInTypeNames.Contains(input) to get the boolean (true/false value) you want.
I don't get this. I was able to cast my first enum value to int but not the second?
public enum PayPalTransactionType
{
Authorization = 0, // Debit
Capture = 1, // Credit
Refund = 2,
Void = 3
}
public string GetPayPalTransCode(PayPalServiceBase.PayPalTransactionType payPalTransactionType)
{
string actionCode = string.Empty;
switch (payPalTransactionType)
{
case (int)PayPalServiceBase.PayPalTransactionType.Authorization:
actionCode = "Debit";
break;
case (int)PayPalServiceBase.PayPalTransactionType.Capture:
actionCode = "Credit";
break;
}
return actionCode;
}
on my 2nd case statement I get this casting error:
Cannot implicitly convert type int
to PayPalTransactionType. An
explicit conversion exists (are you
missing a cast?)
Why are you trying to cast in the first place? Just leave it as the enum value everywhere:
public string GetPayPalTransCode
(PayPalServiceBase.PayPalTransactionType payPalTransactionType)
{
string actionCode = string.Empty;
switch (payPalTransactionType)
{
case PayPalServiceBase.PayPalTransactionType.Authorization:
actionCode = "Debit";
break;
case PayPalServiceBase.PayPalTransactionType.Capture:
actionCode = "Credit";
break;
}
return actionCode;
}
Additionally, I'd have an explicit default action for unrecognised codes, and just return directly:
public string GetPayPalTransCode
(PayPalServiceBase.PayPalTransactionType payPalTransactionType)
{
switch (payPalTransactionType)
{
case PayPalServiceBase.PayPalTransactionType.Authorization:
return "Debit";
case PayPalServiceBase.PayPalTransactionType.Capture:
return "Credit";
default:
return ""; // Or throw an exception if this represents an error
}
}
Alternatively, you could use a Dictionary<PayPalTransactionType, string>.
Why are you casting to int at all? The thing you are switching on is already of the enum type!
As to the other part of the question, the reason the first cast is working is because an implicit cast from a constant int 0 to an enum type always works, whereas the other attempted cast is from a non-zero int value.
why for god's sake are you doing a cast??
public string GetPayPalTransCode(PayPalServiceBase.PayPalTransactionType payPalTransactionType)
{
switch (payPalTransactionType)
{
case (int)PayPalServiceBase.PayPalTransactionType.Authorization:
break;
case (int)PayPalServiceBase.PayPalTransactionType.Capture:
break;
}
}
bascially same type in here, isn't it?! you want to compare enum against enum, don't you?
just do
public string GetPayPalTransCode(PayPalServiceBase.PayPalTransactionType payPalTransactionType)
{
// ...
switch (payPalTransactionType)
{
case PayPalServiceBase.PayPalTransactionType.Authorization:
break;
case PayPalServiceBase.PayPalTransactionType.Capture:
break;
}
// ...
}
btw - it's not best practice to assign PayPalTransactionType.Authorization to 0. 0 should be used for parsing-fallbacks!
edit:
your code would be correct if you do
public string GetPayPalTransCode(PayPalServiceBase.PayPalTransactionType payPalTransactionType)
{
switch ((int)payPalTransactionType)
{
case (int)PayPalServiceBase.PayPalTransactionType.Authorization:
break;
case (int)PayPalServiceBase.PayPalTransactionType.Capture:
break;
}
}
which is quite ...!
Remove the (int) casts in your case statements. The switch can handle the enum values.
Since the switch is on `PayPalTransactionType', values of that type should be used in the case statements.
You don't need these casts in the first place. Just make it case PayPalServiceBase.PayPalTransactionType.Authorization, etc.
Tim,
I am not a Java/C# expert, but here is my 2 cents on the code block above ...
If you're using an enum, why do you need to have an int value, if you do, then do something like:
public enum PayPalTransactionType
{
Authorization(0, Debit),
Capture(1, Credit),
Refund(3, null),
Void(4. null);
private final int code;
public int getCode()
{return(code);}
private PayPalTransactionType(final int code, final TransactionType transactionType)
{
this.code = code;
this.transactionType = transactionType;
}
private TransactionType getTransactionType()
{return(transactionType);}
}
public enum TransactionType
{
Credit("Funds are added to the specified account."),
Debit("funds are deducted from the specified account.");
private final String description;
public String getDescription()
{return(description);}
private TransactionType(final String description)
{this.description = description;}
}
Switches are useful for a very small amount of cases. You can simply do this:
final PayPalTransactionType payPalTransactionType = PayPalTransactionType.Authorization;// this is just an example of the parameter you'd be passing in.
payPalTransactionType.getTransactionType();// an emum is a static database, simply store other things that are related to this value.
Walter
It's not the cast to int that's giving you a problem. Its the cast of (int)PayPalServiceBase.PayPalTransactionType.Capture (which is an int) back to PayPalServiceBase.PayPalTransactionType that's the problem.
You don't need the casts to int. Just use the enum directly.
switch (payPalTransactionType)
{
case PayPalServiceBase.PayPalTransactionType.Authorization:
actionCode = "Debit";
break;
case PayPalServiceBase.PayPalTransactionType.Capture:
actionCode = "Credit";
break;
}
The error occurs because your trying to implicitly convert payPalTransactionType to int. Maybe this will enlighten your problem:
switch ((int)payPalTransactionType)
{
case (int)PayPalServiceBase.PayPalTransactionType.Authorization:
actionCode = "Debit";
break;
case (int)PayPalServiceBase.PayPalTransactionType.Capture:
actionCode = "Credit";
break;
}
This first cast allows you to switch on ints instead of PayPalTransactionTypes. Since the switch statement supports enums, you don't need this cast.
Why are you casting it to an int the first place.? Just do the case statements using the enumerated type.
If you are really just looking up a string a better method would be an array of strings that you index by the enumerated type. That would be faster, less code and easier to maintain.
So something like this:
public enum PayPalTransactionType
{
Authorization = 0, // Debit
Capture = 1, // Credit
Refund = 2,
Void = 3,
Max // This must always be the last type and is used just as a marker.
}
string[] ActionCode = new string[(int)PayPalTransactionType.Max] { "Debit", "Credit", "Refund", "Void" };
public string GetPayPalTransCode(PayPalTransactionType payPalTransactionType)
{
return ActionCode[(int)payPalTransactionType];
}
I have MyClass<T>.
And then I have this string s = "MyClass<AnotherClass>";. How can I get Type from the string s?
One way (ugly) is to parse out the "<" and ">" and do:
Type acType = Type.GetType("AnotherClass");
Type whatIwant = typeof (MyClass<>).MakeGenericType(acType);
But is there a cleaner way to get the final type without any parsing, etc.?
The format for generics is the name, a ` character, the number of type parameters, followed by a comma-delimited list of the types in brackets:
Type.GetType("System.Collections.Generic.IEnumerable`1[System.String]");
I'm not sure there's an easy way to convert from the C# syntax for generics to the kind of string the CLR wants. I started writing a quick regex to parse it out like you mentioned in the question, but realized that unless you give up the ability to have nested generics as type parameters the parsing will get very complicated.
Check out Activator.CreateInstance - you can call it with a type
Activator.CreateInstance(typeof(MyType))
or with an assembly and type name as string
Activator.CreateInstance("myAssembly", "myType")
This will give you an instance of the type you need.
If you need the Type rather than the instance, use the Type.GetType() method and the fully qualified name of the type you're interested in, e.g.:
string s = "System.Text.StringBuilder";
Type myClassType = Type.GetType(s);
That'll give you the Type in question.
I've needed something like this and I ended up writing some code to parse the simple type names I needed. Of course there is room for improvement, as it will not identify generic type names like List<string>, but it does just fine for string, int[], decimal? and such. Sharing in case this helps anyone.
public static class TypeExtensions
{
public static Type GetTypeFromSimpleName(string typeName)
{
if (typeName == null)
throw new ArgumentNullException("typeName");
bool isArray = false, isNullable = false;
if (typeName.IndexOf("[]") != -1)
{
isArray = true;
typeName = typeName.Remove(typeName.IndexOf("[]"), 2);
}
if (typeName.IndexOf("?") != -1)
{
isNullable = true;
typeName = typeName.Remove(typeName.IndexOf("?"), 1);
}
typeName = typeName.ToLower();
string parsedTypeName = null;
switch (typeName)
{
case "bool":
case "boolean":
parsedTypeName = "System.Boolean";
break;
case "byte":
parsedTypeName = "System.Byte";
break;
case "char":
parsedTypeName = "System.Char";
break;
case "datetime":
parsedTypeName = "System.DateTime";
break;
case "datetimeoffset":
parsedTypeName = "System.DateTimeOffset";
break;
case "decimal":
parsedTypeName = "System.Decimal";
break;
case "double":
parsedTypeName = "System.Double";
break;
case "float":
parsedTypeName = "System.Single";
break;
case "int16":
case "short":
parsedTypeName = "System.Int16";
break;
case "int32":
case "int":
parsedTypeName = "System.Int32";
break;
case "int64":
case "long":
parsedTypeName = "System.Int64";
break;
case "object":
parsedTypeName = "System.Object";
break;
case "sbyte":
parsedTypeName = "System.SByte";
break;
case "string":
parsedTypeName = "System.String";
break;
case "timespan":
parsedTypeName = "System.TimeSpan";
break;
case "uint16":
case "ushort":
parsedTypeName = "System.UInt16";
break;
case "uint32":
case "uint":
parsedTypeName = "System.UInt32";
break;
case "uint64":
case "ulong":
parsedTypeName = "System.UInt64";
break;
}
if (parsedTypeName != null)
{
if (isArray)
parsedTypeName = parsedTypeName + "[]";
if (isNullable)
parsedTypeName = String.Concat("System.Nullable`1[", parsedTypeName, "]");
}
else
parsedTypeName = typeName;
// Expected to throw an exception in case the type has not been recognized.
return Type.GetType(parsedTypeName);
}
}
Using it is as simple as writing this:
Type t;
t = TypeExtensions.GetTypeFromSimpleName("string");
t = TypeExtensions.GetTypeFromSimpleName("int[]");
t = TypeExtensions.GetTypeFromSimpleName("decimal?");
To just get the type object from the string, use:
Type mytype = Type.GetType(typeName);
You can then pass this to Activator.CreateInstance():
Activator.CreateInstance(mytype);
I don't have much time to parse through this, though I think I have seen some similar answers. In particular, I think they are doing exactly what you want to do here:
Entity Framework Generic Repository Error
(String.Format("[{0}]", baseType.Name.ToString())).OfType<T>();
Hopefully this helps, let me know more specifically if this isn't.