代码之家  ›  专栏  ›  技术社区  ›  J.G.Sable

在C#/.NET MVC应用程序中从另一个类调用函数

  •  0
  • J.G.Sable  · 技术社区  · 6 年前

    我创建了一个名为 HelperMethods 创建了一个名为 UserHelperMethods 它目前有一个功能:

    namespace HiRatik.Stories.HelperMethods
    {
        public class UserHelperMethods
        {
            //checks if the user's profile pic is null and sets it to default pic if it is
            public string GetUserProfilePic(ApplicationUser user)
            {
                if (user.ProfilePic == null)
                {
                    user.ProfilePic = "profile_pic_default.png";
                }
    
                return user.ProfilePic;
            }
        }
    }
    

    enter image description here

    现在,在控制器的文件夹下,我添加了 using HiRatik.Stories.HelperMethods;

    GetUserProfilePic UserController 用户帮助方法 清理控制器中的大量内容,但我在实现中缺少一些内容。顶部的using语句是灰色的,因此它不接收函数调用。有什么想法吗?

    enter image description here

    5 回复  |  直到 6 年前
        1
  •  1
  •   Jimenemex    6 年前

    您需要将helper方法类的实例添加到要在其中使用它的每个类中。

    UserHelpMethods helper = new UserHelperMethods();
    

    helper.GetUserProfilePic(foundUser);
    ...
    help.DoSomethingImportant(foundUser);
    
        2
  •  1
  •   Kajetan Kazimierczak    6 年前

    user.GetProfilePic();
    

    您必须做的更改是,使类和方法都是静态的,并且 参数前的关键字。像这样的

    public static class ApplicationUserExtensions
        {
            //checks if the user's profile pic is null and sets it to default pic if it is
            public static string GetProfilePic(this ApplicationUser user)
            {
                if (user.ProfilePic == null)
                {
                    user.ProfilePic = "profile_pic_default.png";
                }
    
                return user.ProfilePic;
            }
        }
    
        3
  •  1
  •   Matthew Eskolin    6 年前

    namespace HiRatik.Stories.HelperMethods
    {
        public class UserHelperMethods
        {
            //checks if the user's profile pic is null and sets it to default pic if it is
            public static string GetUserProfilePic(ApplicationUser user)
            {
                if (user.ProfilePic == null)
                {
                    user.ProfilePic = "profile_pic_default.png";
                }
    
                return user.ProfilePic;
            }
        }
    }
    

    如果helper方法不依赖 UserHelperMethods 用户帮助方法 类型。你可以这样调用这个方法。

    UserHelperMethods.GetUserProfilePic(foundUser);
    
        4
  •  0
  •   Saif    6 年前

    只需创建类的实例

    var myInstance = new UserHelperMethods();
    

    myInstance 对象来访问中的函数 UserHelpMethods

    所以你可以调用 这样地

    myInstance.FunctionName();
    

    所以对你来说

    myInstance.GetUserProfilePic(foundUser);
    
        5
  •  0
  •   khaled Dehia    6 年前

    您可以将代码更新为以下内容之一 A-

    namespace HiRatik.Stories.HelperMethods
    {
        public class UserHelperMethods
        {
             private static UserHelperMethods _instance = null;
             public static UserHelperMethods Instance()
             {
               if(_instance == null)
               {
                 _instance = new UserHelperMethods();
               }
               return _instance;
             }
            //checks if the user's profile pic is null and sets it to default pic if it is
            public string GetUserProfilePic(ApplicationUser user)
            {
                if (user.ProfilePic == null)
                {
                    user.ProfilePic = "profile_pic_default.png";
                }
    
                return user.ProfilePic;
            }
        }
    }
    

    在你的控制器里,你可以这样叫它

    UserHelperMethods.Instance().GetUserProfilePic(founduser);
    

    var helperObj = new UserHelperMethods();
    helperObj.GetUserProfilePic(founduser);
    

    您不需要一直在diff控制器中创建实例的diff

    我希望这个能帮助你!!