.net – How to change assembly version of a dll without rebuilding in c#? – Stack Overflow

0down voteacceptedI think you have two options;do not sign the assembly and set specific version to false in the project where you have referenced it.create a Publisher Policy for the assembly and add bindingRedirect element to your configuration.

Source: .net – How to change assembly version of a dll without rebuilding in c#? – Stack Overflow

Advertisements

How to: Enable and Disable Automatic Binding Redirection | Microsoft Docs

How to: Enable and Disable Automatic Binding Redirection2017-3-30 2 min to read Contributors Starting with Visual Studio 2013, when you compile apps that target the .NET Framework 4.5.1, binding redirects may be automatically added to the app configuration file to override assembly unification. Binding redirects are added if your app or its components reference more than one version of the same assembly, even if you manually specify binding redirects in the configuration file for your app. The automatic binding redirection feature affects traditional desktop apps and web apps that target the .NET Framework 4.5.1, although the behavior is slightly different for a web app. You can enable automatic binding redirection if you have existing apps that target previous versions of the .NET Framework, or you can disable this feature if you want to keep manually authored binding redirects.Disabling automatic binding redirects in desktop appsAutomatic binding redirects are enabled by default for traditional desktop apps that target the .NET Framework 4.5.1 and later versions. The binding redirects are added to the output configuration (app.config) file when the app is compiled and overrides the assembly unification that might otherwise take place. The source app.config file is not modified. You can disable this feature by modifying the project file for the app.To disable automatic binding redirectsIn Visual Studio, select the project in Solution Explorer, and then choose Open Folder in File Explorer from the shortcut menu.In File Explorer, find the project (.csproj or .vbproj) file, and open it in Notepad.In the project file, find the following property entry:trueChange true to false:falseEnabling automatic binding redirects manuallyYou can enable automatic binding redirects in existing apps that target older versions of the .NET Framework, or in cases where you are not automatically prompted to add a redirect. If you are targeting a newer version of the framework but do not get automatically prompted to add a redirect, you will likely get build output that suggests you remap assemblies.To manually add an automatic binding redirect propertyIn Visual Studio, select the project in Solution Explorer, and then choose Open Folder in File Explorer from the shortcut menu.In File Explorer, find the project (.csproj or .vbproj) file, and open it in Notepad.Add the following element to the first configuration property group (under the tag):trueThe following shows an example project file with the element inserted.XMLCopy Debug AnyCPU {123334} … true … Compile your app.Enabling automatic binding redirects in web appsAutomatic binding redirects are implemented differently for web apps. Because the source configuration (web.config) file must be modified for web apps, binding redirects are not automatically added to the configuration file. However, Visual Studio notifies you of binding conflicts, and you can add binding redirects to resolve the conflicts. Because you are always prompted to add binding redirects, you do not need to explicitly disable this feature for a web app.To add binding redirects to a web.config fileIn Visual Studio, compile the app, and check for build warnings.If there are assembly binding conflicts, a warning appears. Double-click the warning. (Keyboard: Select the warning and press Enter.)A dialog box that enables you to automatically add the necessary binding redirects to the source web.config file appears.

Source: How to: Enable and Disable Automatic Binding Redirection | Microsoft Docs

C# dynamic ExpandoObject()

using System;
using System.Dynamic;
public class HomeController : Controller
{
       public ActionResult SingleObject()
       {
           dynamic contact = new ExpandoObject(); //Declaring new dynamic object

           contact.Name = "ABC"; // manually adding property & value
           string propertyName = "Age";  int propertyValue = 5; 
           AddProperty(contact, propertyName, propertyValue); //dynamically adding   
                                                                prop & value

           return View(contact);
       }

       //SingleObject.cshtml
       @model dynamic
       <h1>@Model.Name</h1>
       <h1>@Model.Age</h1>

       public ActionResult ObjectList()
       {
          // declaring a list of dynamic object
          dynamic contacts = new List<dynamic>(); 

          // Adding 1st object to the list
          contacts.Add(new ExpandoObject());
          contacts[0].Name = "ABC";
          contacts[0].Phone = "123";

          // Adding 2nd object to the list
          contacts.Add(new ExpandoObject());
          contacts[1].Name = "XYZ";
          contacts[1].Phone = "456";

          return View(contacts);
        }

        //ObjectList.cshtml
        @model dynamic
        @foreach (var item in Model)
        {
           <h2>@item.Name;</h2>
        }

        //Helper method to add property in run-time.
        public void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
        {
            // ExpandoObject supports IDictionary so we can extend it like this
            var expandoDict = expando as IDictionary<string, object>;
            if (expandoDict.ContainsKey(propertyName))
                expandoDict[propertyName] = propertyValue;
            else
                expandoDict.Add(propertyName, propertyValue);
        }
}

Custom Attribute in C#

public class MyCustomAttribute : Attribute
{
    public bool IgnoreIt { get; set; }
    public string FormatString { get; set; }
}

public class Student
{
    [MyCustom(FormatString = "hello", IgnoreIt = false)]
    public string Name { get; set; }

    [MyCustom(IgnoreIt = true)]
    public string Id { get; set; }
}

// Directly check for specific attribute (i.e. FormatString) in specific property 
// (i.e. Name)

var formatString = AttributeHelper.GetPropertyAttributeValue<Student, string, MyCustomAttribute, string>(prop => prop.Name, attr => attr.FormatString.ToString());

public static class AttributeHelper
{
    public static TValue GetPropertyAttributeValue<T, TOut, TAttribute, TValue>(
        Expression<Func<T, TOut>> propertyExpression,
        Func<TAttribute, TValue> valueSelector)
        where TAttribute : Attribute
    {
        var expression = (MemberExpression)propertyExpression.Body;
        var propertyInfo = (PropertyInfo)expression.Member;
        var att = propertyInfo.GetCustomAttributes(typeof(TAttribute), true).FirstOrDefault() as TAttribute;
        return att != null ? valueSelector(att) : default(TValue);
    }
}

//Dynamically check attribute and it's value
PropertyInfo[] props = typeof(Student).GetProperties();
foreach (PropertyInfo prop in props)
{
    var attribute = prop.GetCustomAttributes(typeof(MyCustomAttribute), true).FirstOrDefault() as MyCustomAttribute;
    if (attribute != null)
    {
        if (attribute.FormatString == null)
        {
            Console.WriteLine("null");
        }
        else
        {
            Console.WriteLine(attribute.FormatString);
        }
    }
}

Dynamically enumerate properties from a class

//Let's have a class first- 
public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
}


using System;
using System.Collections.Generic;
using System.Reflection;

//In case of single object
-----------------------------
void EnumerateField<T>(T t)
{
    PropertyInfo[] props = typeof(T).GetProperties();
    foreach (PropertyInfo property in props)
    {
        string propertyName = property.Name.ToString();
        string propertyType = property.PropertyType.ToString();
        string propertyValue = property.GetValue(t).ToString();
    }
}

//Usage-
Student student = new Student() { StudentId = 100, StudentName = "Shindhu-100" };
EnumerateField(student);


//In case of list<T>
-----------------------------------
void EnumerateFields<T>(List<T> ListOfT)
{
    PropertyInfo[] props = typeof(T).GetProperties();
    foreach (var t in ListOfT)
    {
        foreach (PropertyInfo property in props)
        {
            string propertyName = property.Name.ToString();
            string propertyType = property.PropertyType.ToString();
            string propertyValue = property.GetValue(t).ToString();
        }
    }
}

//Usage-
List<Student> students = new List<Student>();
students.Add(new Student() { StudentId = 1, StudentName = "a" });
students.Add(new Student() { StudentId = 2, StudentName = "b" });
Student st = new Student() { StudentId = 100, StudentName = "Shindhu-100" };
students.Add(st);
EnumerateFields<Student>(students);

Split String Based On Upper Case

string ConvertToWord(string value)
{
    StringBuilder builder = new StringBuilder();
    foreach (char c in value)
    {
        if (Char.IsUpper(c) && builder.Length>0)
        {
            builder.Append(' ');
        }

        builder.Append(c);
    }

    string result = builder.ToString();
    return result;
}


//Usage:
string beforeConvert = "IAmAGoodBoy";
string afterConvert = ConvertToWord(beforeConvert); //I Am A Good Boy

Client-Server File Handling Technique

//Server side
[ValidateInput(false)]
[HttpPost]
public JsonResult ReceiveAudio()
{
    try
    {
        foreach (string f in Request.Files.AllKeys)
        {
            var file = Request.Files[f];
            string fileName = file.FileName.ToString();
        }

        return Json("success");
    }
    catch (Exception Ex)
    {

    }
}

Recursively enumerate files within a directory

private void SearchVoiceFiles(string MainDirectoryPath)
{
    try
    {
        foreach (string dir in Directory.GetDirectories(MainDirectoryPath))
        {
            foreach (string file in Directory.GetFiles(dir))
            {
                string fileName = System.IO.Path.GetFileName(file);
                string fileExtenstion = Path.GetExtension(file);
                MessageBox.Show(fileExtenstion);
            }

            SearchVoiceFiles(dir);
        }
    }
    catch (System.Exception)
    {
        throw;
    }
}