Saturday, February 13, 2016

What is Reflection in C# ?


Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules and type information at runtime. In other words, reflection provides objects that encapsulate assemblies, modules and types. A program reflects on itself by extracting metadata from its assembly and using that metadata either to inform the user or to modify its own behavior. Reflection is similar to C++ RTTI (Runtime Type Information), but much broader in scope and capability. By using Reflection in C#, one is able to find out details of an object, method, and create objects and invoke methods at runtime. The System.Reflectionnamespace contains classes and interfaces that provide a managed view of loaded types, methods, and fields, with the ability to dynamically create and invoke types. When writing a C# code that uses reflection, the coder can use the typeof operator to get the object's type or use the GetType() method to get the type of the current instance. Here are sample codes that demonstrate the use of reflection:
Example:

using System; 
using System.Reflection;

 public class MyClass 
  public virtual int Add(int numb1,int numb2) 
   { 
          int result = numb1 + numb2; return result;
    } 

 class MyMainClass 
  public static int Main() 
 { 
     Console.WriteLine ("\nReflection.MethodInfo"); 

    // Create MyClass object 
    MyClass myClassObj = new MyClass();
 
   // Get the Type information. 
   Type myTypeObj = myClassObj.GetType("MyClass");  // we can check which type of class is this

 // Get Method Information.
   MethodInfo myMethodInfo = myTypeObj.GetMethod("Add"); 
 
 object[] mParam = new object[] {5, 10}; 
// Get and display the Invoke method. 

 Console.Write("\nFirst method - " + myTypeObj.FullName + " returns " + myMethodInfo.Invoke(myClassObj, mParam) + "\n"); return 0; 
 } 
}

We can do many things with reflection. Like we can know if property exists in class, class is abstract o not ? , which method have how many parameters ?, we can decide runtime which method to invoke.

For more visit MSDN : https://msdn.microsoft.com/en-us/library/ms173183.aspx

Tuesday, February 9, 2016

What are Strongly Typed Views in MVC



A Strongly Typed view means it has a ViewModel associated to it that the controller is passing to it and all the elements in that View can use those ViewModel properties You can have strongly typed partials as well. Meaning that piece of Html needs specific data so you type it to a certain ViewModel.

 Here is an example of a Strongly Typed View @model SomeViewModel ...// all the html comes after A view that is strongly typed have a @model SomeViewModel line

 Here's one that renders a strongly typed View 

public ActionResult Index()
 {
   var model = new SomeViewModel();
    return View(model); 

And the view makes use of that ViewModel by having the @model SomeViewModel at the top of the file. So now that the view has a ViewModel I can display elements that are bound to the ViewModel like

@Model.Name

@Model.Location

OR for Controls------ @Html.TextBoxFor(m => m.Name) @Html.TextBoxFor(m => m.Location)

Wednesday, December 30, 2015

Started Personal Git Repository

I just pushed whatever i am learning for MVC5 on my personal Git repository-https://github.com/Nirav-Bhadradiya/learn.git
Below is the repository - anyone can point that and look at the stuff. I will be pushing more in next coming days.
Helpful for beginners.!!!! 

Setup Form Authentication in MVC 5

I was just trying Form authentication in MVC5 and i faced some problems while setting it up. i want to share it with you guys so you will not spend your precious time to find out solution.

Step 1: Add below tag in your project's web.config. Remember that you will see one web.config in View folder as well but that is for only View purpose.

<!--  attribute loginUrl is case sensitive please don't change it -->
    <authentication mode="Forms">
      <forms loginUrl="~/Authentication/Login"></forms>
    </authentication>

Here Login URL will be the View where you wanted to redirect your users when they tried accessing unauthorized Action methods.

Step 2; Add below attribute on your methods which you wanted to make secure. in this case i tried with my main action method of one of my Employee.cs class. You can make your whole controller secured with putting Authorize attribute for it.

[Authorize]
        public ActionResult Index()
        {
            var employeeListViewModel = new EmployeeListViewModel {UserName = User.Identity.Name};
            .................................................................
............................................................................
        }

Step 3: Now Modify your Login() Action to set cookies of loggedin user  FormsAuthentication.SetAuthCookie(u.UserName, false);

Step 4: Browse your Action method and it will take you to Login View.



Saturday, October 10, 2015

Fix The ‘Setup Couldn’t Start Properly’ Error When Downloading Windows 10

When you are trying to Upgrade windows 10 and in beetween if something gone wrong and it stopped then if next time you will try to start it it will show below error :

"Setup couldn't start properly.Please reboot your PC and try running windows 10 setup again."

media-creation-tool-error

To solve this issue follow below steps:

Step 1: Browse your drive where system is installed. usually (C drive)

Step 2: Make sure you can see hidden folders and files.

Step 3: Delete the folders named; $Windows.~BT and $Windows.~WS

Step 4: If when performing step 3 it gives any error that you can not delete this folder because it is used by some process then, download Unlocker from here : http://www.filehippo.com/download_unlocker/ 

Step 5: Run Windows Medic Creation Tool and it will again start from scratch.

Saturday, September 26, 2015

Pivot/Unpivot Columns in SQL Server

Today i just used Unpivot Functionality of SQL Server and i want to share with you guys.

What is Pivot and Unpivot ?

You can use the PIVOT and UNPIVOT relational operators to change a table-valued expression into another table. PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where they are required on any remaining column values that are wanted in the final output. UNPIVOT performs the opposite operation to PIVOT by rotating columns of a table-valued expression into column values.


Here is the simple fiddler for the same : http://sqlfiddle.com/#!3/e32bd/1



Saturday, November 30, 2013

Indian entrepreneurs can build next Google: Eric Schmidt

Image

India's entrepreneurial innovators have the potential to build the "next Google" if the country "plays its cards right" and ensures Internet access for millions of its citizens, Google's executive chairman Eric Schmidt has said.

In an essay written for the book 'Reimagining India: Unlocking The Potential of Asia's Next Superpower' edited by global consulting firm McKinsey, Schmidt dubbed India "an Internet laggard" saying he feels Internet in the country today iswhere it was in America in about 1994 - four years before Google was even born.

He said India must increase its Internet penetration across towns and cities, a move that will have a positive impact on its economy and society.

The former Google CEO said he witnessed the creative potential of India's peoplearound him in Silicon ValleyIndia-born entrepreneurs account for 40 per cent of start-ups.

"Just think what will happen when India's entrepreneurial innovators are able to create great global companies without leaving their country. They will change the world. Hundreds of large firms focused on the Internet will be founded and will succeed by focusing purely on Indian consumers, Indian taste, Indian style, Indian sports.

"Can anyone of those companies ultimately become the next Google? Of course."

"That may not happen for quite a few years. But if India plays its cards right, we will soon see Indian engineers and small businesses tackling Indian problems first, then exporting the solutions that work best," Schmidt said.

With a total population of 1.2 billion, India has over 600 million mobile-phone users but only about 150 million people regularly connect to the Internet.

In 2011, India's Internet penetration rate was 11 per cent, "far below" that of developed nationspenetration rates average 70 per cent.

India's Internet penetration rate is less than a third of China's penetration ratio of 38 per cent and less than half of those in developing countries, which average 24 per cent.

"By any reasonable definition, India is an Internet laggard... In spite of its well deserved reputation as one of the world's leading IT and software development hubs, India is farbeing the connected society many foreigners imagine," Schmidt said.

The number of India's broadband users, 20 million, is even smaller, Schmidt said however adding that India is on the cusp of a connectivity revolution.

"I believe India has the chance to leapfrog its current connectivity challenges, bring Internet access to a majority of its citizens - and even raise its penetration ratio to 60 or 70 per cent within the next 5-10 years," he said.