InternetException

C#

C# · 28 posts (all categories)

Disable Xunit Test on specific OS

· 253 words · ~1 min read · in .NET, C#, debian, linux, testing

#Unit tests

Lately I was having a problem with some of my tests causing problems for other developers on the project I am working on. They were using Windows for their development while I am using Debian for my work. The application that was being tested was not designed for non Unix system - now rarely anybody is hosting they production code on Windows Server - everybody is using Unix for .net.

Read the whole thing →

Writing a simple C# source code generator with Fluent API

· 1812 words · ~9 min read · updated · in .NET, C#

#source generators #NuGet

Introduction

Usually in .NET world when you want to write generic code you use generic method or classes. But sometimes it is still not enough when you want to automate some things. I.e. you want to dynamically register in DI all the classes that implements some interface. Probably, in this case, you would do something like that:

var implementations = typeof(MyClass).Assembly.Types
   .Where(t => t.GetInterfaces().Any(i => i == typeof(IMyInterface))
foreach(var implementation in implementations)
{
   services.AddScoped<IMyInterface>(implementation);
}

And it would be completely fine. Even if Reflection is pretty slow, your application would not be getting significantly slower because of that. Certainly you would not notice the slow down.

Read the whole thing →

Automatic scrolling to edited text box in Xamarin Android.

· 858 words · ~4 min read · updated · in .NET, Android, C#, Xamarin

Important note: this should work well with newer version of Xamarin (2.3.3). If you cannot update for some reason this article will explain workaround.

Introduction

In previous article I explained how to detect show and hide events of software keyboard in Xamarin Android. I did it in my project because of the fact that some of text boxes was hidden by software keyboard that suppose to edit contents of those text boxes. In this article I will explain how to work around this issue in some older versions of Xamarin.

Read the whole thing →

Xamarin with TinyIoC

· 3190 words · ~15 min read · updated · in .NET, Android, C#

#XAML #dependency injection #Xamarin

Introduction

This article is builds on topics and solutions introduces in ViewFactory for Xamarin application and Xamarin Master-Detail Page articles. It shows method for easier navigation and use of services via IoC in Xamarin application. Knowledge from previous examples is not necessary, but makes this article bit easier to understand.

Read the whole thing →

Genereting proxies during runtime using Reflection.Emit

· 4156 words · ~20 min read · updated · in .NET, C#, WCF

#reflection #self-hosting #delegates #XAML

Reflection.Emit is very powerful tool. It creates IL code and since C# is converted into IL too, we have the same functionality as in C# and even more. It is very powerful and at the same time very complicated. Because of that it is worth to discuss how and for what it should be used. One idea is to create dynamic code with automatic implementations of interfaces - proxy types.

Read the whole thing →

Faster than Reflection: Delegates. Part 3.

· 6868 words · ~34 min read · updated · in .NET, C#

#delegates #reflection #generics #NuGet

This is third article in series, that explains how to create delegates for public and non-public members of public, non-public or not known in compile-time types. If you are not familiar with previous articles and looking for detailed explanation of how delegates works, how they are created and why, I strongly encourage you to read first two. If you are just looking for a way to create delegates for methods or constructors it is not necessary. Code with new features and with bug fixes is available on GitHub and as Nuget package.

Read the whole thing →

Faster than Reflection: Delegates. Part 2.

· 5330 words · ~26 min read · updated · in .NET, C#

#delegates #reflection #NuGet

This is second article in series, that explains how to create delegates for public and non-public members of public, non-public or not known in compile-time types. If you are not familiar with previous article and looking for detailed explanation of how delegates works, how they are created and why, I strongly encourage you to read it first. If you are just looking for a way to create delegates for methods or constructors it is not necessary. Last article in series you can find here . Code with new features and with bug fixes is available on GitHub and as Nuget package. In previous article and first one in series, we covered:

  1. Static
    1. Properties
    2. Fields
  2. Instance
    1. Properties
    2. Fields
    3. Indexers

Now it is time to cover following members.

  1. Static
    1. Methods
  2. Instance
    1. Methods
    2. Constructors

Read the whole thing →

Faster than Reflection: Delegates. Part 1.

· 10477 words · ~52 min read · updated · in .NET, C#

#delegates #reflection #NuGet

This article is first part of the series of three. Next ones you can find here and here . Code for all three articles, with new features and bug fixes is available on GitHub and as Nuget package.

Why not Reflection?

If you working with .NET and C# (probably other languages too), at some point, you will have to write more advanced and generic code, that i.e. reads all properties of a type, searches for attributes, etc. Or maybe you want to call some private method or property? It is not that rare to use some internal mechanism of framework or library. Usually, the first solution involves Reflection. But the thing is: Reflection is slow. Of course, it is sufficient if you need to use Reflection rarely and it is not the main code of the application. If it is used more often at some point, you will probably try to optimize it. And now is the fun part: how to do it?

Read the whole thing →

Xamarin Master-Detail Page

· 5408 words · ~27 min read · updated · in .NET, C#

#XAML #design patterns #Xamarin

This article as previous ones (Frame With Padding , Frame Outline Problem , Inheritance from Implicit Styles ) also will be about Xamarin. This time we will inspect Master-Detail page control/mechanism. You can find official documentation for this control here . Quick spoiler: I never used this in any production application. Why? I will explain. Whole article will be to explain why this is just not worth your time. In this and subsequent articles I will introduce much better solution.

Read the whole thing →

Xamarin Style based on implicit Style

· 1251 words · ~6 min read · updated · in .NET, C#, Xamarin

#XAML #WPF

Next article about bugs in Xamarin will touch topic of Styles. Styles for all of controls not only Frame (and if you wondering why Frame you should check first two articles about Xamarin here and here) . First of all if you are using Styles in Xamarin you probably know how to define implicit styles. If not here is a sample:

<Application.Resources>
  <ResourceDictionary>
    <Style TargetType="Label">
      <Setter Property="FontSize" Value="45" />
    </Style>
  </ResourceDictionary>
</Application.Resources>

Read the whole thing →

Xamarin Frame and disappearing Outline

· 777 words · ~3 min read · updated · in .NET, Android, C#, Xamarin

#XAML

This is another article in mini series of Xamarin bugs and workarounds for them :) Previous one you can find here . This one is again about Frame control. I promise that another one will be about something else B) Issue can be observed when you bind BackgroundColor property of Frame control and something will trigger change of view model source property binded to background color.

Read the whole thing →

Xamarin Frame and Style with Padding

· 496 words · ~2 min read · updated · in .NET, Android, C#, Xamarin

#XAML

If you are a bit advanced with Xamarin you should already now that there is possibility of styling your controls almost like in WPF. Almost, because there is a few strange bugs involving this. For example there is a problem with styling a Padding property in Frame. Consider following Xaml of Xamarin application (I tested it in Android):

Read the whole thing →

Safe cast string to enum

· 1039 words · ~5 min read · updated · in .NET, C#

#enums

In on of my projects I had make an interface between database and web service in C# code. One of problem I had to face was need to cast strings to enumerable types, because of simple fact that database have no idea what is an ’enum’. Yes simplest mapping between an enumeration and database type is integer. It is simplest but in my idea not best. For example I really do not remember (or do not WANT to remember) what is the meaning of 4 integer value in case of package shipment status.

Read the whole thing →

Smart string builder

· 579 words · ~2 min read · updated · in .NET, C#

In this rare times when I was writing code that suppose to create large string from smaller chunks and with some non string parameters I was using StringBuilder class of course. Of course because string adding, or string concatenation is very costly when talking about memory utilization. It is because every time you do this:

var text = "Hello world!"+ "n"+ "How are you?";

new string is created for every ‘+’ operation in memory. Not a best way of doing strings creations. StringBuilder is better because it do not creates strings until you call .ToString method of StringBuilder class.

Read the whole thing →

Expression parsing and nested properties

· 729 words · ~3 min read · updated · in .NET, C#

In one of projects I was working on, I needed to get property value from property path and property path from expression. First, lets cover second case. With expression in form of nested property value:

()=>object1.object2.object3.object4

we cannot take simple value of some property, because we have only root object, object1.

Read the whole thing →

Ninject and Entity Framework

· 730 words · ~3 min read · updated · in .NET, C#, Entity Framework

#Ninject

Last time I wrote about problem with testing Entity Framework in Visual Studio 2012. During working on this project I also encounter problems with configuration of Ninject to allow injecting Entity Framework classes from its Patterns extension. To be frankly this was not much a problem then lack of documentation. I just was forced to experiment with Ninject and new-thing-for-me Entity Framework Patterns extension.

Read the whole thing →

Using Matlab from C# application.

· 1606 words · ~8 min read · updated · in .NET, C#, Matlab

While writing an application for my Masters degree diploma, I wrote a simple application that was using COM Matlab server. I have found it hard to use it mainly due to lack of documentation, which is really basic with only few code examples for C#. I guess writing programs that use Matlab for calculating is not encouraged by MathWorks, you would became competition that way :) Nonetheless, I accomplished why I was required to do, so I decided to share this with the rest of the world.

Read the whole thing →

Server controls in separete assembly part 2

· 795 words · ~3 min read · updated · in .NET, ASP.NET, C#

Yesterday I was writing about creating server controls in separate assembly . Today I will cover more complicated example then simple “Hello World!” control. My goal was to create text box that will take handler method to run when validation event will be triggered, and when validation fail will show appropriate message. Text box with builtin validation would be nice, since you always should validate user input , right? But lets start creating our control.

Read the whole thing →

Server controls in separate assembly

· 394 words · ~1 min read · updated · in .NET, ASP.NET, C#

In my project I wanted to create text box control with builtin configurable validation. ASP.NET definition in .aspx or .ascx file would get validation function by name. But I do not wanted to create just .ascx file with text box. I could not then reuse that control in other projects and it certainly would be nice, right? Creating such server control from code is not hard, but maintainability of HTML code in C# (it cannot be done other way) is not good. It’s pain really. Why Microsoft would create way to automatically create C# code from .ascx, .aspx files is mystery for me.

Read the whole thing →

Better algorithm for finding UpdatePanel that will be updated during page request.

· 496 words · ~2 min read · updated · in .NET, ASP.NET, C#

#UpdatePanel

Other day I was experimenting with finding which UpdatePanel will be updated during page post back. I found out that my solution was not working correctly for complex triggers, like user controls with custom events, which will trigger async postbacks and update of panel. Or for grids with buttons. It was big issue. Also it was not pointing to correct UpdatePanel if name of control was not entirely unique (for example when you have multiple same custom controls on page). It would return first panel, even if it wasn’t updating.

Read the whole thing →

Display message on client from anywhere in code with ASP.NET

· 890 words · ~4 min read · updated · in .NET, ASP.NET, C#

#UpdatePanel

When I was working with WinForms application in previous work I have found one of most useful feature, ability to show some message to the client. For example user cannot edit some entity in particular time (it is being edited by someone else) or should change password because it will expire in x days. I wanted to do something similar in my web application.

Read the whole thing →

Getting private field or property from object

· 201 words · ~1 min read · updated · in .NET, C#

Yesterday I was playing with finding all UpdatePanel controls on page during generating response in ASP.NET. Since object with this information is private itself and collection of panels is private in that object, I had to write some code to extract that data. I ended up with to extension methods for Object type, for getting private field an private property. It’s pretty straightforward, besides one thing: since member is private. And we don’t know in witch type exactly (every type in base/descendant hierarchy have it’s one private members) we have to search them all. So this is final code:

Read the whole thing →